[phobos] Time to get ready for the next release

Steve Schveighoffer schveiguy at yahoo.com
Fri Apr 22 05:43:45 PDT 2011




>________________________________
>From: David Simcha <dsimcha at gmail.com>
>To: Discuss the phobos library for D <phobos at puremagic.com>
>Sent: Thursday, April 21, 2011 7:36 PM
>Subject: Re: [phobos] Time to get ready for the next release
>
>
>The fluent interface is my biggest concern.  Enforcing @property with strict semantics makes it impossible to use a fluent interface and property syntax for the same field.  Again, in Plot2kill, the following pieces of code are equivalent and IMHO should be:
>
>// Code piece 1
>auto hist = Histogram(someArray, 100);
>hist.barColor = getColor(255, 0, 0);
>auto fig = hist.toFigure;
>fig.title = "A histogram";
>fig.xLabel = "XXX";
>
>// Code piece 2:
>auto fig = Histogram(someArray, 100)
>    .barColor(getColor(255, 0, 0))
>    .toFigure
>    .title("A histogram")
>    .xLabel("XXX");
>
>I consider this a very nice API.  The with statement isn't a good substitute.  I think it's ridiculous to break things like this in the name of ultra-rigid consistency or to solve a few weird corner cases w.r.t. functions that return zero-argument delegates, especially when the zero-argument delegate corner case can be solved by enforcing @property with loose semantics.

There are a couple problems with this.

First, there is the WTF factor when you want to set multiple colors with the same value:

hist1.barColor = hist2.barColor = getColor(255, 0, 0);

WTF? this is an error? But this works (enjoy the clarity of this):

hist1.barColor = (hist2.barColor = getColor(255.0, 0)).barColor;

Second, it looks strange to have a function named barColor.  In an extreme interpretation, I could expect that with barColor, I am barring that color from the object ;)  Not that it would be a common interpretation, but the name looks more ambiguous than say, setBarColor.  There are certainly better cases that could demonstrate my point, as English is full of words that are both nouns or adjectives and also verbs.


My belief is that the function style functions should be differently named from the property style functions, so there is no chance for misinterpretation, and that works fine with strict properties.

-Steve



More information about the phobos mailing list