mixin alias
Derek Parnell
derek at nomail.afraid.org
Sun Dec 16 18:32:44 PST 2007
On Mon, 17 Dec 2007 03:07:22 +0100, 0ffh wrote:
> Derek Parnell wrote:
>> On Sun, 16 Dec 2007 15:51:38 -0500, Jarrett Billingsley wrote:
>>
>>> "Derek Parnell" <derek at psych.ward> wrote in message
>>> news:jnak6l8ihhe1.k3tzm3in813z.dlg at 40tude.net...
>>>> Is there any reason why the alias template parameter cannot be a literal?
>>>>
>>>>
>>>> template Foo(alias b) {
>>>> int X = b;
>>>> }
>>>>
>>>> void main() {
>>>> int y = 4;
>>>> mixin Foo!(y); // This is okay
>>>> mixin Foo!(4); // This fails to compile
>>>> // "mixin Foo!(4) does not match any template declaration"
>>>> }
>>>>
>>> This is correct. You can only alias things that have names; the number 3
>>> does not have a name.
>>
>> I know that is 'correct', but I was really asking what is the rationale
>> behind such an apparently arbitrary design decision.
>
> Well, for one we'd have to disallow assignment expressions using the
> aliased parameter (as well as pre- and post-increment)
Why? If you had such a template (one that used 'side effects') but supplied
a literal, it would/should fail at the mixin site. I can't see why that
necessarily prevents the same template being used with non-literals, as
your example below demonstrates.
> template Foo(alias b)
> {
> int X=(b=4);
> }
>
> void main()
> {
> int a=3;
> printf("%i\n",a); // 3
> mixin Foo!(a); // side effect here
> printf("%i\n",a); // yeas, it's 4 now
> printf("%i\n",X); // 4
mixin Foo!(5) // Should fail because 'int X=(5=4);' is bad syntax.
> }
IMHO, such a template needs *heavy* justification as do all such 'hidden'
side effects code. Buts that's just the way I think.
--
Derek
(skype: derek.j.parnell)
Melbourne, Australia
17/12/2007 1:27:49 PM
More information about the Digitalmars-d-learn
mailing list