handful and interval

Timon Gehr timon.gehr at gmx.ch
Mon Sep 3 12:15:46 PDT 2012


On 09/03/2012 05:05 PM, bearophile wrote:
> David Nadlinger:
>
>> Where would the real difference to ["struct", "class",
>> "union"].canFind(a) then? A sufficiently smart compiler (tm) could
>> optimize this to a efficient string "prefix switch statement" just as
>> well…
>
> I think a smart compiler is not needed.
>
> Maybe a general solution is to introduce a way to define overloaded
> templates callable as functions, that get called if the input is a
> literal (or statically known, but D design requires you to asks
> explicitly for a CT evaluation):
>
> void foo(static int[] a) {}
> void foo(int[] a) {}
> void main(string[] args) {
>      foo([1, 2]); // calls first foo
>      int[] a = [1, 2] ~ args.length;
>      foo(a); // calls second foo
> }
>
>
> Bye,
> bearophile

It is not general enough. (and static is the wrong keyword.)

This would eventually lead to a solution like

bool among(S,T...)(S needle, auto enum T haystack){
     ...
     foreach(h;haystack) static if(__traits(isConstant, h)){ ... }
     ...
}

Which is still rather specific.

Anyway, I don't consider among trying to be clever crucial at all.


More information about the Digitalmars-d mailing list