shortcut for dynamic dispatch and operators
Michel Fortin
michel.fortin at michelf.com
Tue Dec 1 13:53:12 PST 2009
On 2009-12-01 14:01:26 -0500, Andrei Alexandrescu
<SeeWebsiteForEmail at erdani.org> said:
> KennyTM~ wrote:
>> Alternative suggestion:
>>
>> Make "x in y" returns a bool and works for arrays. Then you can write
>>
>> int opBinary(string s)(int rhs) if (s in ["+", "-", "*", "/", "^", "|",
>> "&"]) { ... }
>
> It's a bit difficult to see a very thin operator mask a linear
> operation, but I'm thinking maybe "x in y" could be defined if y is a
> compile-time array. In that case, the compiler knows the operation and
> the operand so it may decide to change representation as it finds fit.
I don't like making this compile-time only. There are many cases where
you may want to do the same check at runtime and it'd be a bother to
need a different syntax. Better to create a wrapper struct for such
arrays:
s in set("+", "-", "*", "/", "^", "|", "&")
where set() returns a Set!string struct wrapping a sorted array of
those operators for which 'in' could work fast even at runtime. Make
sure it works with CTFE and you have what you want.
--
Michel Fortin
michel.fortin at michelf.com
http://michelf.com/
More information about the Digitalmars-d
mailing list