Few things II

BCS ao at pathlink.com
Thu Aug 16 10:01:39 PDT 2007


Reply to bearophile,

> Sorry for my answering delay.
> 
> BCS:
> 
>> I'll put a copy at the end that has some edits, mostly for code space
>> and editablility.<
>> 
> Thank you. I'm learning D, so I can comment your code; this is the
> first line: Xpermutations!(TyItem) xpermutations(TyItem)(TyItem[]
> items, bool copy=true)
> 
> I think it has a problem, this templated class (that't the resulting
> object of the helper function) doesn't receive the boolean value:
> 
> Xpermutations!(TyItem)

that is because it can't, not directly at least. This is because the argument 
is a runtime value and template parameter must be compile time values. However 
because bools can only have two values it is trivial to generate both versions 
and have the run time code select between them.

OTOH your correct about the return type. this could be solved by using an 
abstract base class that uses the (TyItem) and a derived class that uses 
both args.
Another solution would be to return a delegate to the opApply function:

|int delegate(int delegate(ref TyItem[])) xpermutations(TyItem)(TyItem[] 
items, bool copy=true)
|{
| if(copy)
| return &(new Xpermutations!(TyItem, true)(items)).opApply;
| else
| return &(new Xpermutations!(TyItem, false)(items)).opApply;
|}
 
> And this line:
> if (int result = dg2(items2)) return result;
> May raise an error like this:
> modulename.d(linenumber): Error: '=' does not give a boolean result

it doesn't. assignment expressions cause that error, variable delectations 
don't

> 
> (Maybe a language like D can be designed to have some hi-level
> constructs to be used in the spots where you don't care of speed;
> beside other lower lever ones to be used where you need more speed and
> you need to exactly know what's happening under the cover. Other
> people may argue you can't fit both things in the same language).
> 

I tend to think low level. C# annoys the heck out of me because it's "to 
high level". ff





More information about the Digitalmars-d mailing list