roles #88
Replies: 4 comments 28 replies
-
|
Lacking the time to add it right this moment, but it is also important that a role must be able to define something that adds an argument to the consuming class' constructor. |
Beta Was this translation helpful? Give feedback.
-
|
Discussing this irl yesterday with leonerd, the question came up why it has to be roles doing this thing. Wouldn't it also work to just be able to combine existing classes to form new classes? I had no objections at the time. |
Beta Was this translation helpful? Give feedback.
-
|
@leonerd @wchristian here is my proposal for roles in core OO. |
Beta Was this translation helpful? Give feedback.
-
|
I have recently been using roles a lot in my own modules, primarily with Role::Tiny, I have even added integration code that allows Object::HashBase to play nicely with Role::Tiny. For me the big 2 things I use a lot are required methods, and Default implementations. Required methods guaranteeing an interface is available is the biggest one, and the "this thing will work in this slot" is why I use it. By default implementations I meanm instead of making a blank required method, I implement a default version of the method that should work most of the time, and the classes that consume the role get it unless they override it. This avoids needing to write the same code in many places or add a new utility class. The Object::HashBase integration I did was mainly because I also needed attributes, not just methods (yeah the distinction is thin in perl). Object::HashBase exports constants for all the attributes you define with it, allowing I have found that I am avoiding Also a relevant note on inheritance vs roles when using an LLM, the moment you bring attributes intot he picture on perl the LLMs seem to give up using roles entirely and write base classes instead. I suspect this may have more to do with me using Object::HashBase + Role::Tiny instead of Moo/Moose. But there could be something more fundamental there, or maybe it is an artifact of perl not supporting roles as well for so long. This also is not something the perl built-in role system should consider or address, it si just a note that some may find interesting. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
@leonerd asked for people to provide pseudocode demonstrating how people expect roles to work, here's an example to start the conversation.
Beta Was this translation helpful? Give feedback.
All reactions