« 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 Jorge Ortiz-3 :: Rate this Message:

Reply to Author | View in Thread

This sounds dangerously close to some dangerous monkey-patching.

  package java.lang

  object String {
    def new() = ... // I'm in ur compiler, hackin' ur stringz
  }

Maybe String is a bad example because you rarely use "new String" explicitly, but you get the idea.

Also, I think this lends itself to more WTFs/minute (http://www.osnews.com/story/19266/WTFs_m) for people unfamiliar with the feature.

Consider:

"Someone is calling this constructor, but I don't see it defined anywhere. WTF?"

vs.

"I just added a new constructor to this class, but my code is mysteriously doing something else instead. WTF? WTF!"

The former might lead me to seek out someone more knowledgeable with Scala, while the latter might lead me to bang at the code for three hours in an attempt to figure it out.

--j

On Mon, Mar 23, 2009 at 4:09 PM, Ben Hutchison <benh@...> wrote:
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?

-Ben

--

       

*Ben Hutchison
Senior Developer
* Level 2 476 St Kilda Road Melbourne VIC 3004
T 613 8807 5252 | F 613 8807 5203 | M 0423 879 534 | www.ibsglobalweb.com <http://www.ibsglobalweb.com/>



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