couple of really noob questions (ranges, toString)
Michael Woods
alienhunter3 at gmail.com
Mon Nov 1 13:02:14 PDT 2010
Hi. To describe my background really quickly, I'm a CS student with experience mostly in Java and Python. (I've done some C++ work, but not a lot.)
Basically, I have two really newbish questions that I haven't been able to figure out from the library and language references, and I'm sure the answers
are perfectly obvious, so apologies in advance.
I'm writing a few collections classes in D (just to get the feel for the language; I realize that there are probably much better implementations available
from the dsource community, or w/e) and right now, I'm writing a linked list template class. ( 'linkedList(E)' )
First question: toString(). is this handled exactly the way that it is in Java? I've written a toString method for my class, I'll post it below. I can
call this method, and it works perfectly, if called specifically.
linkedList!(int) list = new linkedList!(int)();
writeln(list.toString()); //this works perfectly (if I add elements to the list, the results are the same)
writeln(list); //this fails(at compile time) with a godawfully long error:
"/usr/local/include/d/phobos2/std/format.d(1455): Error: template std.format.formatValue(Writer,T,Char) if (is(const(T) == const(void[])))
formatValue(Writer,T,Char) if (is(const(T) == const(void[]))) matches more than one template declaration,
/usr/local/include/d/phobos2/std/format.d(1126):formatValue(Writer,T,Char) if (isInputRange!(T) && !isSomeChar!(ElementType!(T))) and
/usr/local/include/d/phobos2/std/format.d(1297):formatValue(Writer,T,Char) if (is(T == class))"
Anyway, my toString method is as follows:
public string toString(){
if (head !is null){
char[] outString = "[".dup;
llNode cursor = head; //llNode is a private node class
while(cursor.next !is null){
outString = outString ~ text(cursor.datum) ~", ";
cursor = cursor.next;
}
outString = outString ~ text(cursor.datum) ~ "]";
return outString.idup;
}
else{
return "[]";
}
}
I guess that I'm asking if write() calls an object's toString method, or if there's some other behavior going on that I'm ignorant of.
Question 2:
This is more a broad request for clarification, than a specific question. I'm trying to understand the range interfaces. I'm trying to make this
linkedList class implement the InputRange interface (among others). I'm pretty sure that I've got opApply, opIndex, and opSlice figured out, but I can't
for the life of me figure out exactly what front(), popFront(), and empty() are supposed to be doing. (I realize that the opX functions aren't part of the
InputRange interface.) Is popFront supposed to delete the actual front element from the list? Or is it supposed to represent some kind of internal
pointer that is there solely for the purpose of range functionality? If the latter, how should the pointer get reset after it has gotten to the end once?
Should the pointer even need to be reset, or is popFront supposed to only cycle through once during the entire lifetime of the range object?
I really have tried to answer this question for myself, but I guess that I need more experience to actually be able to understand all of the documentation
the way that it is currently written. Also, I've attached the d source files in question. One is the linkedList class, one is the test executable that I'm
trying to test it with. linkedList.d should be in a directory mwcollections (since i'm trying to set up mwcollections as a module). I'd have just sent an
archive file with this directory structure already in place, but I wouldn't want anyone to think that I was slipping them a virus. :) (though 4KB is a
little small to be a virus.) Feel free to crack jokes at my poor coding style, I'm not really much more than an amateur at this. I don't know if this is
relevant or not, but I'm using the digitalmars dmd compiler version 2.050, on a gentoo linux system.
Thanks in advance.
Mike W
begin 644 testlist.d
M:6UP;W)T('-T9"YS=&1I;SL*:6UP;W)T('-T9"YR86YG93L*:6UP;W)T(&UW
M8V]L;&5C=&EO;G,N;&EN:V5D3&ES=#L*"G9O:60@;6%I;BAS=')I;F=;72!A
M<F=S*7L*"B`@(`H@("`@;&EN:V5D3&ES="$H:6YT*2!L:7-T(#T@;F5W(&QI
M;FME9$QI<W0A*&EN="DH*3L*("`@(&QI<W0N<'5T*#$I.PH@("`@;&ES="YP
M=70H,BD["B`@("!L:7-T+G!U="@S*3L*("`@(`H@("`@:6YT6S-=('D@/2!;
M-"P at -2P@-ET["B`@("!L:7-T+G!U="AY*3L*("`@(`H@("`@+RH*("`@(&9O
M<F5A8V at H:6YT(&L[>"E[("`O+VIU<W0@=&\@9&5M;VYS=')A=&4@=&AA="!O
M<$%P<&QY(&ES('=O<FMI;F<N"B`@("`@("`@=W)I=&5L;BAK*3L*("`@('T*
M("`@("HO"B`@("`*("`@('=R:71E;&XH;&ES="YT;U-T<FEN9R at I*3L@("`O
M+V-O;7!I;&5S"B`@("\O('=R:71E;&XH;&ES="D[("`@("`@("`@("`@("`O
7+V1O97-N)W0 at 8V]M<&EL90H@("`*?0H`
`
end
begin 644 linkedList.d
M;6]D=6QE(&UW8V]L;&5C=&EO;G,N;&EN:V5D3&ES=#L*"FEM<&]R="!S=&0N
M<W1D:6\["FEM<&]R="!S=&0N<F%N9V4["FEM<&]R="!S=&0N8V]N=CL*"G!U
M8FQI8R!C;&%S<R!L:6YK961,:7-T*$4I(#H at 26YP=71286YG92$H12E["B`@
M("`*("`@("\O<')I=F%T92!N;V1E(&-L87-S. at H@("`@"B`@("!P<FEV871E
M(&-L87-S(&QL3F]D92!["B`@("`@("`@"B`@("`@("`@<'5B;&EC($4 at 9&%T
M=6T["B`@("`@("`@"B`@("`@("`@<'5B;&EC(&QL3F]D92!N97AT.PH@("`@
M("`@(`H@("`@("`@('!U8FQI8R!L;$YO9&4@<')E=FEO=7,["B`@("`@("`@
M"B`@("`@("`@<'5B;&EC('1H:7,H12!?9&%T=6TL(&QL3F]D92!?;F5X="P@
M;&Q.;V1E(%]P<F5V:6]U<RE["B`@("`@("`@("`@(`H@("`@("`@("`@("!D
M871U;2`](%]D871U;3L*("`@("`@("`@("`@"B`@("`@("`@("`@(&YE>'0@
M/2!?;F5X=#L*("`@("`@("`@("`@"B`@("`@("`@("`@('!R979I;W5S(#T@
M7W!R979I;W5S.PH@("`@("`@("`@("`*("`@("`@("!]"B`@("`@("`@"B`@
M("!]"B`@("`*("`@("\O:6YT97)N86P@=F%R<PH@("`@"B`@("!L;$YO9&4@
M:&5A9#L*("`@(`H@("`@;&Q.;V1E('1A:6P["B`@("`*("`@('5I;G0@<VEZ
M93L*("`@(`H@("`@;&Q.;V1E(&-U<G)E;G0["B`@("`*("`@(`H@("`@"B`@
M("`O+V-O;G-T<G5C=&]R"B`@("`*("`@('!U8FQI8R!T:&ES*"E["B`@("`@
M("`@"B`@("`@("`@<VEZ92`](#`["B`@("`@("`@"B`@("`@("`@:&5A9"`]
M(&YU;&P["B`@("`@("`@"B`@("`@("`@=&%I;"`](&YU;&P["B`@("`@("`@
M"B`@("`@("!C=7)R96YT(#T@:&5A9#L*("`@("`@("`*("`@('T*("`@(`H@
M("`@"B`@("`O+W!U8FQI8R!M96UB97(@9G5N8W1I;VYS"B`@("`*("`@('9O
M:60@<'5T*$4 at 7VEN<'5T*7L*("`@("`@("`*("`@("`@("!I9BAH96%D(&ES
M(&YU;&PI>PH@("`@("`@("`@("!H96%D(#T@;F5W(&QL3F]D92A?:6YP=70L
M(&YU;&PL(&YU;&PI.PH@("`@("`@("`@("!S:7IE*RL["B`@("`@("`@("`@
M('1A:6P@/2!H96%D.PH@("`@("`@("`@("!C=7)R96YT(#T@:&5A9#L*("`@
M("`@("`@("`@"B`@("`@("`@?0H@("`@("`@(`H@("`@("`@(&5L<V5["B`@
M("`@("`@("`@(`H@("`@("`@("`@("!T86EL+FYE>'0@/2!N97<@;&Q.;V1E
M*%]I;G!U="P@;G5L;"P@=&%I;"D["B`@("`@("`@("`@(`H@("`@("`@("`@
M("!T86EL(#T@=&%I;"YN97AT.PH@("`@("`@("`@("`*("`@("`@("`@("`@
M<VEZ92LK.PH@("`@("`@("`@("`*("`@("`@("!]"B`@("`@("`@"B`@("!]
M"B`@("`*("`@('!U8FQI8R!V;VED('!U="A%6UT at 7VEN<'5T*7L*("`@("`@
M("`*("`@("`@("!F;W)E86-H*$4@:SL at 7VEN<'5T*7L*("`@("`@("`@("`@
M<'5T*&LI.PH@("`@("`@('T*("`@("`@("`*("`@('T*("`@(`H@("`@<'5B
M;&EC($4 at 9G)O;G0H*2!["B`@("`@("`@<F5T=7)N(&-U<G)E;G0N9&%T=6T[
M"B`@("!]"B`@("`*("`@('!U8FQI8R!%(&UO=F5&<F]N="@I('L@<F5T=7)N
M(&AE860N9&%T=6T[?0H@("`@"B`@("!P=6)L:6,@=F]I9"!P;W!&<F]N="@I
M('L*("`@("`@("!C=7)R96YT(#T at 8W5R<F5N="YN97AT.PH@("`@?3L*("`@
M(`H@("`@<'5B;&EC(&)O;VP at 96UP='DH*2![(`H@("`@("`@("`@("!I9B`H
M8W5R<F5N="!I<R!N=6QL*7L*("`@("`@("`@("`@("`@(')E='5R;B!T<G5E
M.PH@("`@("`@("`@("!]"B`@("`@("`@("`@(&5L<V5["B`@("`@("`@("`@
M("`@("!R971U<FX at 9F%L<V4["B`@("`@("`@("`@('T*("`@('T*("`@(`H@
M("`@<'5B;&EC(&EN="!O<$%P<&QY*&EN="!D96QE9V%T92AR968 at 12D@9&<I
M>R`*("`@("`@("`*("`@("`@("!I;G0@<F5S=6QT(#T@,#L*("`@("`@("!L
M;$YO9&4 at 8W5R<V]R(#T@:&5A9#L*("`@("`@("!W:&EL92AC=7)S;W(@(6ES
M(&YU;&PI>PH@("`@("`@("`@("!R97-U;'0@/2!D9RAC=7)S;W(N9&%T=6TI
M.PH@("`@("`@("`@("!I9BAR97-U;'0@(3T@,"E["B`@("`@("`@("`@("`@
M("!B<F5A:SL*("`@("`@("`@("`@?0H@("`@("`@("`@("!C=7)S;W(]8W5R
M<V]R+FYE>'0["B`@("`@("`@?2`*("`@("`@(')E='5R;B!R97-U;'0["B`@
M("!]"B`@("`*("`@('!U8FQI8R!I;G0@;W!!<'!L>2AI;G0 at 9&5L96=A=&4H
M<F5F('-I>F5?="P@<F5F($4I(&1G*7L@"B`@("`@("`@:6YT(')E<W5L="`]
M(#`["B`@("`@("`@;&Q.;V1E(&-U<G-O<B`](&AE860["B`@("`@("`@=6EN
M="!C=7));F1E>"`](#`["B`@("`@("`@=VAI;&4H8W5R<V]R("%I<R!N=6QL
M*7L*("`@("`@("`@("`@<F5S=6QT(#T at 9&<H8W5R26YD97 at L(&-U<G-O<BYD
M871U;2D["B`@("`@("`@("`@(&EF*')E<W5L="`A/2`P*7L*("`@("`@("`@
M("`@("`@(&)R96%K.PH@("`@("`@("`@("!]"B`@("`@("`@("`@(&-U<G-O
M<CUC=7)S;W(N;F5X=#L*("`@("`@("`@("`@8W5R26YD97 at K*SL*("`@("`@
M("!]"B`@("`@("!R971U<FX@<F5S=6QT.PH@("`@?0H@("`@"B`@("`*("`@
M(`H@("`@<'5B;&EC($4@;W!);F1E>"AS:7IE7W0 at 7VEN9&5X*7L*"B`@("`@
M("`@:68@*%]I;F1E>"`\('-I>F4@)B8 at 7VEN9&5X(#X](#`I>PH@("`@("`@
M("`@("`*("`@("`@("`@("`@;&Q.;V1E(&-U<G-O<CL*("`@("`@("`@("`@
M:6YT(&-U<DEN9&5X.PH@("`@("`@("`@("`*("`@("`@("`@("`@:68@*%]I
M;F1E>"`\('-I>F4O,BE["B`@("`@("`@("`@("`@("`*("`@("`@("`@("`@
M("`@(&-U<G-O<B`](&AE860["B`@("`@("`@("`@("`@("!C=7));F1E>"`]
M(#`["B`@("`@("`@("`@("`@("!W:&EL92AC=7));F1E>"`A/2!?:6YD97 at I
M>PH@("`@("`@("`@("`@("`@("`@(&-U<G-O<B`](&-U<G-O<BYN97AT.PH@
M("`@("`@("`@("`@("`@("`@(&-U<DEN9&5X*RL["B`@("`@("`@("`@("`@
M("!]"B`@("`@("`@("`@("`@("!R971U<FX at 8W5R<V]R+F1A='5M.PH@("`@
M("`@("`@("!]"B`@("`@("`@("`@(&5L<V5["B`@("`@("`@("`@("`@("!C
M=7)S;W(@/2!T86EL.PH@("`@("`@("`@("`@("`@8W5R26YD97@@/2!S:7IE
M+3$["B`@("`@("`@("`@("`@("!W:&EL92AC=7));F1E>"`A/2!?:6YD97 at I
M>PH@("`@("`@("`@("`@("`@("`@(&-U<G-O<B`](&-U<G-O<BYP<F5V:6]U
M<SL*("`@("`@("`@("`@("`@("`@("!C=7));F1E>"TM.PH@("`@("`@("`@
M("`@("`@?0H@("`@("`@("`@("`@("`@<F5T=7)N(&-U<G-O<BYD871U;3L*
M("`@("`@("`@("`@?0H@("`@("`@('T*("`@("`@("!E;'-E>PH@("`@("`@
M("`@("!T:')O=R!N97<@17AC97!T:6]N*"));F1E>"!O=70@;V8 at 8F]U;F1S
M+B(I.PH@("`@("`@('T*("`@("`@("`*("`@('T*("`@(`H@("`@<'5B;&EC
M(&QI;FME9$QI<W0A*$4I(&]P4VQI8V4H*7L*("`@("`@("!L:6YK961,:7-T
M(2A%*2!T96UP(#T@;F5W(&QI;FME9$QI<W0A*$4I*"D["B`@("`@("`@;&Q.
M;V1E(&-U<G-O<B`](&AE860["B`@("`@("`@=VAI;&4H8W5R<V]R("%I<R!N
M=6QL*7L*("`@("`@("`@("`@=&5M<"YP=70H8W5R<V]R+F1A='5M*3L*("`@
M("`@("`@("`@8W5R<V]R(#T at 8W5R<V]R+FYE>'0["B`@("`@("`@?0H@("`@
M("`@(')E='5R;B!T96UP.PH@("`@?0H@("`@"B`@("!P=6)L:6,@;&EN:V5D
M3&ES="$H12D@;W!3;&EC92AS:7IE7W0@;&]W+"!S:7IE7W0@:&EG:"E["B`@
M("`@("`@"B`@("`@("`@:68H;&]W(#X@<VEZ92TQ('Q\(&AI9V@@/B!S:7IE
M+3$@?'P@;&]W(#X@:&EG:"E["B`@("`@("`@("`@('1H<F]W(&YE=R!%>&-E
M<'1I;VXH(DEN9&5X*&5S*2!O=70@;V8 at 8F]U;F1S+B(I.PH@("`@("`@('T*
M("`@("`@("!E;'-E>PH@("`@("`@("`@("!L:6YK961,:7-T(2A%*2!T96UP
M(#T@;F5W(&QI;FME9$QI<W0A*$4I*"D["B`@("`@("`@("`@(&QL3F]D92!C
M=7)S;W(@/2!H96%D.PH@("`@("`@("`@("!I;G0 at 8W5R26YD97@@/2`P.PH@
M("`@("`@("`@("!W:&EL92AC=7));F1E>#QL;W<I>PH@("`@("`@("`@("`@
M("`@8W5R26YD97 at K*SL*("`@("`@("`@("`@("`@(&-U<G-O<B`](&AE860N
M;F5X=#L*("`@("`@("`@("`@?0H@("`@("`@("`@("!W:&EL92AC=7));F1E
M>"`\/2!H:6=H*7L*("`@("`@("`@("`@("`@('1E;7`N<'5T*&-U<G-O<BYD
M871U;2D["B`@("`@("`@("`@("`@("!C=7)S;W(@/2!C=7)S;W(N;F5X=#L*
M("`@("`@("`@("`@("`@(&-U<DEN9&5X*RL["B`@("`@("`@("`@('T*("`@
M("`@("`@("`@<F5T=7)N('1E;7`["B`@("`@("`@?0H@("`@("`@(`H@("`@
M?0H@("`@"B`@("!P=6)L:6,@<W1R:6YG('1O4W1R:6YG*"E["B`@("`@("`@
M"B`@("`@("`@"B`@("`@("`@:68@*&AE860@(6ES(&YU;&PI>PH@("`@("`@
M("`@("!C:&%R6UT@;W5T4W1R:6YG(#T@(ELB+F1U<#L*("`@("`@("`@("`@
M;&Q.;V1E(&-U<G-O<B`](&AE860[("`O+VQL3F]D92!I<R!A('!R:79A=&4@
M;F]D92!C;&%S<PH@("`@("`@("`@("!W:&EL92AC=7)S;W(N;F5X="`A:7,@
M(&YU;&PI>PH@("`@("`@("`@("`@("`@;W5T4W1R:6YG(#T@;W5T4W1R:6YG
M('X@=&5X="AC=7)S;W(N9&%T=6TI('XB+"`B.PH@("`@("`@("`@("`@("`@
M8W5R<V]R(#T at 8W5R<V]R+FYE>'0["B`@("`@("`@("`@('T*("`@("`@("`@
M("`@;W5T4W1R:6YG(#T@;W5T4W1R:6YG('X@=&5X="AC=7)S;W(N9&%T=6TI
M('X@(ETB.PH@("`@("`@("`@("!R971U<FX@;W5T4W1R:6YG+FED=7`["B`@
M("`@("`@?0H@("`@("`@(&5L<V5["B`@("`@("`@("`@(')E='5R;B`B6UTB
M.PH@("`@("`@('T*("`@("`@("`*("`@("`@("`*("`@('T*("`@(`H@("`@
."B`@("`*"B`@("`*?0H`
`
end
More information about the Digitalmars-d-learn
mailing list