Using lazily ?
Ali Çehreli
acehreli at yahoo.com
Thu Mar 1 14:43:56 PST 2012
On 03/01/2012 02:25 PM, bioinfornatics wrote:
> Le jeudi 01 mars 2012 à 23:10 +0100, Timon Gehr a écrit :
>> S s;
>> size_t tokenLength = "member1".length;
>> void main(){
>> foreach(char[] line; stdin.byLine())
>> foreach(m;__traits(allMembers,S)){
>> if(line[0..tokenLength] == m) mixin("s."~m) =
>> line[tokenLength
>> .. $].idup;
>> }
>> }
>
> awesome :)
>
> can use hasMember instead allMembers ?
No, because both of those are compile-time features. Even if you used
hasMember, the answer will always be true:
if (__traits(hasMember, S, "member1"))
Yes, S has member1.
Note that Timon's inner foreach is a compile-time foreach, which is the
equivalent of the following three lines:
if(line[0..tokenLength] == "member1") s.member1 = line[tokenLength ..
$].idup;
if(line[0..tokenLength] == "member2") s.member2 = line[tokenLength ..
$].idup;
if(line[0..tokenLength] == "member3") s.member3 = line[tokenLength ..
$].idup;
There is no inner foreach looop that is executed at runtime.
Ali
More information about the Digitalmars-d-learn
mailing list