"Robert Jacques" <sandf...@jhu.edu> wrote in message news:op.vrvz7do126s...@sandford.myhome.westell.com... > On Sat, 05 Mar 2011 13:42:32 -0500, bearophile <bearophileh...@lycos.com> > wrote: > >> dsimcha: >> >>> I've done some major updating of my Plot2kill plotting library lately, >> >> I see code that wants named arguments :-) >> >> 65 auto sleepinessFig = Figure(sleepinessPlot) >> 66 .title("Sleepiness Survey") >> 67 .yLabel("Sleepiness Rating") >> 68 .xLabel("Activity") >> 69 .legendLocation(LegendLocation.right) >> 70 .horizontalGrid(true) >> 71 .xTickLabels( >> 72 iota(3), >> 73 ["In Meeting", "On Phone", "Coding"] >> 74 ); >> >> Bye, >> bearophile > > Why? Each of those arguments should be able to be set after creation. So > given DRY principals, method chaining is the way to go. Besides, Figure > takes a variable number of plot arguments, which means you couldn't > support default arguments (at least with the current syntax).
I've also attached this as a file in case it's hard to read on the NG clients: -------------------------------------------------- // Tested on DMD 2.050 import std.variant; template Optional(T) { alias Algebraic!(T) Optional; } template notSpecified(T) { Optional!T notSpecified; } Optional!T specified(T)(T val) { return Optional!T(val); } void setIfSpecified(T)(ref T target, Optional!T val) { if(val.hasValue()) target = val.get!T(); } struct Plot { string name; } struct Fancy { string memberVar1; int[] memberVar2; bool memberVar3; // I don't know why I need this or why it seems to need to be called manually. const int opCmp(ref const Fancy other) { return memberVar1 == other.memberVar1 && memberVar2 == other.memberVar2 && memberVar3 == other.memberVar3; } } struct Figure { Plot plot; string title; bool horizontalGrid; Fancy fancy; this(Plot plot) { this.plot = plot; } ref Figure set( Optional!string title = notSpecified!string, Optional!bool horizontalGrid = notSpecified!bool, Optional!Fancy fancy = notSpecified!Fancy ) { setIfSpecified!string(this.title, title); setIfSpecified!bool (this.horizontalGrid, horizontalGrid); setIfSpecified!Fancy (this.fancy, fancy); return this; } } void main() { // All of the ugly 'specified()' could be eliminated if there // were some sort of opImplicitCast auto fig = Figure(Plot("My Plot")) .set( specified("My Title"), specified(true), specified(Fancy("Foo", [1,2,3], true)) ); assert(fig.title == "My Title"); assert(fig.horizontalGrid == true); assert(fig.fancy.opCmp( Fancy("Foo", [1,2,3], true) )); fig = fig.set().set().set(); assert(fig.title == "My Title"); assert(fig.horizontalGrid == true); assert(fig.fancy.opCmp( Fancy("Foo", [1,2,3], true) )); fig = fig.set( specified("New title name") ); assert(fig.title == "New title name"); assert(fig.horizontalGrid == true); assert(fig.fancy.opCmp( Fancy("Foo", [1,2,3], true) )); // The above maybe aren't so special as-is, but with named arguments // the following could be uncommented and used: /+ fig = fig.set( fancy: specified(Fancy("Bar", [7,9,12], false)), title: specified("Another Title") ); assert(fig.title == "Bar"); assert(fig.horizontalGrid == true); assert(fig.fancy.opCmp( Fancy("Bar", [7,9,12], false) )); +/ } -------------------------------------------------- begin 666 plotStuff2.d M+R\@5&5S=&5D(&]N($1-1" R+C U, T*:6UP;W)T('-T9"YV87)I86YT.PT* M#0IT96UP;&%T92!/<'1I;VYA;"A4*0T*>PT*"6%L:6%S($%L9V5B<F%I8R$H M5"D@3W!T:6]N86P[#0I]#0H-"G1E;7!L871E(&YO=%-P96-I9FEE9"A4*0T* M>PT*"4]P=&EO;F%L(50@;F]T4W!E8VEF:65D.PT*?0T*#0I/<'1I;VYA;"%4 M('-P96-I9FEE9"A4*2A4('9A;"D-"GL-"@ER971U<FX@3W!T:6]N86PA5"AV M86PI.PT*?0T*#0IV;VED('-E=$EF4W!E8VEF:65D*%0I*')E9B!4('1A<F=E M="P@3W!T:6]N86PA5"!V86PI#0I[#0H):68H=F%L+FAA<U9A;'5E*"DI#0H) M"71A<F=E=" ]('9A;"YG970A5"@I.PT*?0T*#0IS=')U8W0@4&QO= T*>PT* M"7-T<FEN9R!N86UE.PT*?0T*#0IS=')U8W0@1F%N8WD-"GL-"@ES=')I;F<@ M;65M8F5R5F%R,3L-"@EI;G1;72 @;65M8F5R5F%R,CL-"@EB;V]L(" @;65M M8F5R5F%R,SL-"@D-"@DO+R!)(&1O;B=T(&MN;W<@=VAY($D@;F5E9"!T:&ES M(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