Re: MX::Traits why can't traits be added with just new?
[email protected] (Caleb Cushing)
| Newsgroups | perl.moose |
|---|---|
| Message-ID | <CAAHKNRFC+wAiZ9rCnT3+hd8yv53USJvGDi2PvO5KP9VA0Li+-w@mail.gmail.com> |
On Wed, Mar 14, 2012 at 4:57 AM, Tomas Doran <[email protected]> wrote: > > On 14 Mar 2012, at 02:14, Caleb Cushing wrote: > >> Why are the options for using MX::Traits either >> >> Class->with_traits(...) or Class->new_with_traits({ traits => ... }); >> >> why can't we have just a Class->new({ _traits => ... })? > > Because then ->new returns an entirely different class to the one you asked for. > > Which breaks people's expectations of what ->new does. one could argue that if you're programming to a concrete class vs an interface ... you're doing it wrong (I'm sure that's an opinion). The problem I'm having is having to program for 2 interfaces. > Also - this is hard to make work 'right', as what happens with classes which implement their own BUILDARGS / BUILD to do custom argument processing? How would we hook into that to get { _traits } out before construction, etc.. You don't? if someone adds { _traits } then in BUILDARGS you compose it like you would if they did it in new_with_traits, and IIRC the class is composed by the time you get to BUILD, and so if the class is immutable you can't add them at that point. BUILDARGS needs to return a hashref, if you need a custom argument processing, and _traits, then you need to deal with that in your BUILDARGS somehow. The problem I'm having is that I've been wanting to add traits at ->new, including in things like MX::AbstractFactory (and I think maybe this is why Bread::Board doesn't support Interface Injection? though maybe idk what that actually means, ignore if I'm completely off base ). The problem is that because MX::Traits doesn't allow me (even optionally) to use plain old new to do this, then I have to add all kinds of code to handle the different interface, and explain to people "Don't use new, it won't do what you need". What if I want to add traits in BUILDARGS? perhaps there's another way to do it, but it seems like it's probably 10 times cludgier. I'm not saying new_with_traits is bad, or with_traits is bad, just that I'd like to be able optionally turn on a way to do it with just ->new . regardless of whether it changes peoples expectations of what new does (because otherwise I have to change a different expectation, which construction method to use ) -- Caleb Cushing http://xenoterracide.com