« Return to Thread: Draft SIP: From constructors to factories: Uniform treatment of object creation

Re: Draft SIP: From constructors to factories: Uniform treatment of object creation

by David MacIver :: Rate this Message:

Reply to Author | View in Thread

2009/3/23 Ben Hutchison <benh@...>:

> David MacIver wrote:
>>
>> Summary: Make it easy to switch from constructors to factories after
>> the fact by adding a mechanism where "new Foo(stuff)" can translate to
>> an invocation of a user defined method on the companion object.
>>
>> http://www.drmaciver.com/factories-sip.xhtml
>>
>
> Generally, I like the idea of being able to switch from new Foo() to
> Foo.new().
>
> I have a question about your SIP. It seems that you propose that, when the
> compiler encounters new Foo(x, y), if and only if c'tor signature Foo(x, y)
> is not defined does it look for factory method Foo.new(x, y).
>
> Presumably you considered the alterative that it looks for Foo.new(x, y)
> first, then if not found, new Foo(x, y)? This would allow overriding c'tors
> with a factory without removing the affected c'tor. Why do you prefer the
> former option?

Hm. That's a good question.

Offhand I can't think of a reason (minor boring quibbles about
backwards compatibility in edge cases aside). Let me have a think
about that and I'll get back to you.

 « Return to Thread: Draft SIP: From constructors to factories: Uniform treatment of object creation