<?xml version='1.0' encoding='UTF-8'?><?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/'><id>tag:blogger.com,1999:blog-18428163.post285419773902035610..comments</id><updated>2008-04-06T22:47:19.264+02:00</updated><category term='C++'/><category term='tedir'/><category term='v8'/><category term='eclipse'/><category term='java'/><category term='neutrino'/><category term='misc'/><category term='saturn'/><category term='neptune'/><title type='text'>Comments on Hummus and Magnets: Constructors #2</title><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://blog.quenta.org/feeds/285419773902035610/comments/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/18428163/285419773902035610/comments/default'/><link rel='alternate' type='text/html' href='http://blog.quenta.org/2007/12/constructors-2.html'/><author><name>Christian Plesner Hansen</name><uri>https://profiles.google.com/111760511997911681005</uri><email>noreply@blogger.com</email><gd:image xmlns:gd='http://schemas.google.com/g/2005' rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='//lh4.googleusercontent.com/-iqVJZTnTt6s/AAAAAAAAAAI/AAAAAAAABe4/25zxZv9THv8/s512-c/photo.jpg'/></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>5</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>25</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-18428163.post-7981608406764640023</id><published>2008-04-06T22:47:00.000+02:00</published><updated>2008-04-06T22:47:00.000+02:00</updated><title type='text'>Hi Burak, thanks for your comments.&lt;br&gt;&lt;br&gt;I do ha...</title><content type='html'>Hi Burak, thanks for your comments.&lt;BR/&gt;&lt;BR/&gt;I do have a lot of opinions on Scala for someone who hasn't really ever used it, don't I.  In this case, I didn't actually know about the &lt;B&gt;new Bar { ... }&lt;/B&gt; mechanism.  It does indeed look very similar and seems to avoid partial initialization problems (modulo the behavior of Bar's constructors).  However, as far as I understand the mechanism it only works for leaf classes, you have to use standard constructors to build deeper inheritance hierarchies.  Is there a way to use this to express the example from the post where both Point2D and Point3D are free to switch their internal representation without requiring changes to each other or client code, and where partial initialization can't occur?&lt;BR/&gt;&lt;BR/&gt;(I'm curious -- if you're not a fan of object orientation do you know of an alternative solution to this problem in a different paradigm?)&lt;BR/&gt;&lt;BR/&gt;Extractors do give the data abstraction I think is missing with case classes; I also like the pattern matching mechanism in F#.  I'm not sure either solution gets it exactly right but on the other hand I can't think of any way to improve them.&lt;BR/&gt;&lt;BR/&gt;Even though &lt;B&gt;new&lt;/B&gt; methods are no different from others and you might as well have called &lt;B&gt;Point.at(x: 0, y: 0)&lt;/B&gt; I think the code becomes easier to read if people use the same method name more or less consistently.  The special syntax is there to encourage that, and to make construction expressions stand out more.  But in the language I'm working on syntax is cheap -- the new expression is not built in, it's just a convenience added by a library.&lt;BR/&gt;&lt;BR/&gt;You're right about the translation of &lt;B&gt;new&lt;/B&gt; methods and, in the simple case, the implementation of keyword arguments is relatively straightforward.  It's a little more complicated than tuples since you have to be able to reorder the arguments.  Calling &lt;B&gt;f(a: 3, b: 4)&lt;/B&gt; and &lt;B&gt;f(b: 4, a: 3)&lt;/B&gt; should have the same effect except for the evaluation order of the arguments.</content><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/18428163/285419773902035610/comments/default/7981608406764640023'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/18428163/285419773902035610/comments/default/7981608406764640023'/><link rel='alternate' type='text/html' href='http://blog.quenta.org/2007/12/constructors-2.html?showComment=1207514820000#c7981608406764640023' title=''/><author><name>Christian Plesner Hansen</name><uri>http://www.blogger.com/profile/02224596590597033051</uri><email>noreply@blogger.com</email><gd:image xmlns:gd='http://schemas.google.com/g/2005' rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='http://bp3.blogger.com/_1GHaebjyeU8/R-pWu7ogXzI/AAAAAAAAABI/xmBNZ0uWC_I/S220/shaun-the-black-headed-sheep.jpg'/></author><thr:in-reply-to xmlns:thr='http://purl.org/syndication/thread/1.0' href='http://blog.quenta.org/2007/12/constructors-2.html' ref='tag:blogger.com,1999:blog-18428163.post-285419773902035610' source='http://www.blogger.com/feeds/18428163/posts/default/285419773902035610' type='text/html'/><gd:extendedProperty xmlns:gd='http://schemas.google.com/g/2005' name='blogger.itemClass' value='pid-32251972'/></entry><entry><id>tag:blogger.com,1999:blog-18428163.post-79341437285199655</id><published>2008-04-06T18:52:00.000+02:00</published><updated>2008-04-06T18:52:00.000+02:00</updated><title type='text'>Christian, I feel like adding here a couple of poi...</title><content type='html'>Christian, I feel like adding here a couple of points&lt;BR/&gt;&lt;BR/&gt;1. To Scala's defense, a lot of design choices and limitations stem from the we-will-always-compile-to-JVM doctrine. This also includes that you should be knowing what you should not be doing when you call a virtual method from within a constructor.&lt;BR/&gt;&lt;BR/&gt;2. In addition to those limitations, Scala's virtual types are directly tied to constructors - if a class has a type member Foo, then (similar to generics), one has  to make a choice for "type Foo". Syntax like &lt;B&gt;new Bar { type Foo = String; val x = 3; val y = 4 }&lt;/B&gt; is pretty elegant, consistent and straightforward and I find your comments regarding Scala constructors do not seem to cover this side of object construction (which looks somewhat similar to your own).&lt;BR/&gt;&lt;BR/&gt;3. Sometimes I think un-object-oriented is a compliment, but more to the point, I agree that case classes are in conflict with data abstraction, that is why there are extractors now to provide a more fine-grained approach to pattern matching. In newer versions of Scala, a case class is translated to a static method call behind the scenes (case class Bar becomes object Bar with an "apply" method and a class Bar). The apply method plays the same role as the static factory methods.&lt;BR/&gt;&lt;BR/&gt;4. (This could answer the question of the previous comment) If new Foo is the same as Foo.new, why provide syntax for it?&lt;BR/&gt;&lt;BR/&gt;BTW, an answer for the question in the previous comment, if it is the same as in Scala, a constructor call &lt;B&gt;new Foo(arg1,...,argN) &lt;/B&gt;becomes &lt;B&gt;Foo.new(arg1,...,argN)&lt;/B&gt;. For Scala case classes, a call &lt;B&gt;Foo(arg1,...,argN)&lt;/B&gt; becomes &lt;B&gt;Foo.apply(arg1,...,argN)&lt;/B&gt;. I guess making it work with named parameters is not hard, as fixed-length records are really the same as tuples.</content><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/18428163/285419773902035610/comments/default/79341437285199655'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/18428163/285419773902035610/comments/default/79341437285199655'/><link rel='alternate' type='text/html' href='http://blog.quenta.org/2007/12/constructors-2.html?showComment=1207500720000#c79341437285199655' title=''/><author><name>Burak Emir</name><uri>http://www.blogger.com/profile/01651698653168460072</uri><email>noreply@blogger.com</email><gd:image xmlns:gd='http://schemas.google.com/g/2005' rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:in-reply-to xmlns:thr='http://purl.org/syndication/thread/1.0' href='http://blog.quenta.org/2007/12/constructors-2.html' ref='tag:blogger.com,1999:blog-18428163.post-285419773902035610' source='http://www.blogger.com/feeds/18428163/posts/default/285419773902035610' type='text/html'/><gd:extendedProperty xmlns:gd='http://schemas.google.com/g/2005' name='blogger.itemClass' value='pid-100230662'/></entry><entry><id>tag:blogger.com,1999:blog-18428163.post-7139539190047929120</id><published>2008-01-03T16:04:00.000+01:00</published><updated>2008-01-03T16:04:00.000+01:00</updated><title type='text'>If new X() is equivalent to X.new(), then what is ...</title><content type='html'>If new X() is equivalent to X.new(), then what is new X() { x: 3, y: 4 } equivalent to?</content><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/18428163/285419773902035610/comments/default/7139539190047929120'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/18428163/285419773902035610/comments/default/7139539190047929120'/><link rel='alternate' type='text/html' href='http://blog.quenta.org/2007/12/constructors-2.html?showComment=1199372640000#c7139539190047929120' title=''/><author><name>ahe</name><uri>http://www.blogger.com/profile/10190680778403319748</uri><email>noreply@blogger.com</email><gd:image xmlns:gd='http://schemas.google.com/g/2005' rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='25' src='http://blogs.sun.com/ahe/resource/peter-ahe-20060716.jpg'/></author><thr:in-reply-to xmlns:thr='http://purl.org/syndication/thread/1.0' href='http://blog.quenta.org/2007/12/constructors-2.html' ref='tag:blogger.com,1999:blog-18428163.post-285419773902035610' source='http://www.blogger.com/feeds/18428163/posts/default/285419773902035610' type='text/html'/><gd:extendedProperty xmlns:gd='http://schemas.google.com/g/2005' name='blogger.itemClass' value='pid-1325713882'/></entry><entry><id>tag:blogger.com,1999:blog-18428163.post-8260082664609040823</id><published>2008-01-02T21:58:00.000+01:00</published><updated>2008-01-02T21:58:00.000+01:00</updated><title type='text'>I'm not much of a fan actually.&lt;br&gt;&lt;br&gt;In Scala, a...</title><content type='html'>I'm not much of a fan actually.&lt;BR/&gt;&lt;BR/&gt;In Scala, as in Java, you have free access to the object being initialized during initialization and you can execute arbitrary code in context of that object.  That has some unfortunate consequences.  For instance, if a superclass constructor happens to invoke a method that has been overridden in a subclass then the method will be executed before the subclass has had a chance to initialize itself. This can cause some very subtle and unintuitive bugs, especially if there is no immediate error but you just silently propagate some inconsistent values.  And of course if initialization code happens to throw an exception then you get a broken object.  This model also makes it impossible to have real immutable fields and only a more complicated approximation, like &lt;B&gt;final&lt;/B&gt;s in java, is possible.&lt;BR/&gt;&lt;BR/&gt;The part worst part of scala constructors, though, is the way they are tied to pattern matching in case classes.  There you get direct access to the object's fields which ties code not only to a particular implementation of an object but to the way in which the object was constructed.  That seems very un-object-oriented.</content><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/18428163/285419773902035610/comments/default/8260082664609040823'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/18428163/285419773902035610/comments/default/8260082664609040823'/><link rel='alternate' type='text/html' href='http://blog.quenta.org/2007/12/constructors-2.html?showComment=1199307480000#c8260082664609040823' title=''/><author><name>Christian Plesner Hansen</name><uri>http://www.blogger.com/profile/02224596590597033051</uri><email>noreply@blogger.com</email><gd:image xmlns:gd='http://schemas.google.com/g/2005' rel='http://schemas.google.com/g/2005#thumbnail' width='30' height='32' src='http://data.quenta.org/detail.jpg'/></author><thr:in-reply-to xmlns:thr='http://purl.org/syndication/thread/1.0' href='http://blog.quenta.org/2007/12/constructors-2.html' ref='tag:blogger.com,1999:blog-18428163.post-285419773902035610' source='http://www.blogger.com/feeds/18428163/posts/default/285419773902035610' type='text/html'/><gd:extendedProperty xmlns:gd='http://schemas.google.com/g/2005' name='blogger.itemClass' value='pid-32251972'/></entry><entry><id>tag:blogger.com,1999:blog-18428163.post-3483705312817121015</id><published>2007-12-31T19:54:00.000+01:00</published><updated>2007-12-31T19:54:00.000+01:00</updated><title type='text'>How do you feel about Scala's approach to construc...</title><content type='html'>How do you feel about Scala's approach to constructors?</content><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/18428163/285419773902035610/comments/default/3483705312817121015'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/18428163/285419773902035610/comments/default/3483705312817121015'/><link rel='alternate' type='text/html' href='http://blog.quenta.org/2007/12/constructors-2.html?showComment=1199127240000#c3483705312817121015' title=''/><author><name>Neal Gafter</name><uri>http://www.blogger.com/profile/08579466817032124881</uri><email>noreply@blogger.com</email><gd:image xmlns:gd='http://schemas.google.com/g/2005' rel='http://schemas.google.com/g/2005#thumbnail' width='28' height='32' src='http://www.gafter.com/~neal/gafter-google.jpg'/></author><thr:in-reply-to xmlns:thr='http://purl.org/syndication/thread/1.0' href='http://blog.quenta.org/2007/12/constructors-2.html' ref='tag:blogger.com,1999:blog-18428163.post-285419773902035610' source='http://www.blogger.com/feeds/18428163/posts/default/285419773902035610' type='text/html'/><gd:extendedProperty xmlns:gd='http://schemas.google.com/g/2005' name='blogger.itemClass' value='pid-1689238302'/></entry></feed>
