Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding same role multiple times #1941

Open
s1037989 opened this issue May 16, 2022 · 1 comment
Open

Adding same role multiple times #1941

s1037989 opened this issue May 16, 2022 · 1 comment

Comments

@s1037989
Copy link
Contributor

$ mojo version
CORE
  Perl        (v5.34.1, linux)
  Mojolicious (9.25, Waffle)

OPTIONAL
  Cpanel::JSON::XS 4.09+   (4.28)
  EV 4.32+                 (4.33)
  IO::Socket::Socks 0.64+  (0.74)
  IO::Socket::SSL 2.009+   (2.074)
  Net::DNS::Native 0.15+   (0.22)
  Role::Tiny 2.000001+     (2.002004)
  Future::AsyncAwait 0.52+ (0.58)

Steps to reproduce the behavior

This isn't a bug because it's not doing something contrary to documentation, but seems it could be an improvement.

$ perl -Mojo -E 'say ref f("/tmp")->with_roles("+Extension")'
Mojo::File__WITH__Mojo::File::Role::Extension
$ perl -Mojo -E 'say ref f("/tmp")->with_roles("Mojo::File::Role::Extension")->with_roles("Mojo::File::Role::Extension")'
Mojo::File__WITH__Mojo::File::Role::Extension__WITH__Mojo::File::Role::Extension

Expected behavior

I would think it would be ideal for with_roles to detect the role has already been added and skip it.

Actual behavior

Currently with_roles adds the role even though it's already been added.

Furthermore, using the shortcut is even more bizarre:

$ perl -Mojo -E 'say ref f("/tmp")->with_roles("+Extension")->with_roles("+Extension")'
Can't locate Mojo/File__WITH__Mojo/File/Role/Extension/Role/Extension.pm in @INC
@Grinnz
Copy link
Contributor

Grinnz commented May 16, 2022

You could use https://metacpan.org/pod/With::Roles to apply roles as it keeps track of this within its own usage. Unfortunately it's not a feature of Role::Tiny itself so it would depend on using the same Role::Tiny wrapper to apply every role.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants