Manifest constants (was const again)

Jari-Matti Mäkelä jmjmak at utu.fi.invalid
Sun Dec 9 05:35:54 PST 2007



On Sat, 8 Dec 2007, Bill Baxter wrote:

> Simen Kjaeraas wrote:
>> Oskar Linde wrote:
>>> Janice Caron wrote:
>>>>> alias str = "str";
>>>>  I'm uncomfortable with having
>>>>      alias dest = source;
>>>>  in one circumstance, but
>>>>      alias source dest;
>>>>  in others. I would find that confusing.
>>> 
>>> Then just make the two forms equivalent and maybe depreciate the second 
>>> one with time.
>>> 
>>> alias pi = 3.14;
>>> alias toString = to!(string);
>>> 
>>> Why would this be a problem?
>>> 
>> 
>> Because it breaks with the C/C++ heritage, methinks.
>
> I hate to point out the obvious, but there is no "alias" in C or C++.
> Ok, yes there's typedef in C, but if you go and completely change the keyword 
> used, I think you are justified in changing the syntax.

I think it's also worth pointing out that 'alias' in D isn't just 
C/C++'s weak typedef. For example alias also includes the functionality of 
references:

   int a;
   int &b = a;

is in D

   int a;
   alias a b;

Having 'alias b = a' would be more consistent with that.

>
>> Anyways, is there a reason why we can't use 'alias 3.14 pi;'?
>
> Things like
>   alias 3.14 + ctfe_func("two") / other_constant pi;
>
> make that much harder to read than
>
>   alias pi = 3.14 + ctfe_func("two") / other_constant;
>
> But the same is true for the current type aliases.  You can see it a lot in 
> templates.  There its not uncommon to see things like
>
>  alias some long thing that eventually figures out a type Foo;

Thanks for bringing this up too. I think I saw Andrei commenting in some 
bug report that template alias params should handle also string literals. 
Now if you generalize it enough, alias could point to an AST subgraph. 
This is something you have in certain functional programming languages, 
and it's a powerful concept. Of course FPLs might be a curse word here, 
but I just wanted to mention it. The 'foo = bar' makes it clearer what 
part is lhs and what part rhs and maybe possibly D could extend it later 
to also include more than what it does now.

I think the alias should work so that it doesn't reserve storage space for 
the symbols and also make use of polysemous values, if possible. Possibly 
auto/const variables could be used if also ro/rw storage needs to be 
provided.



More information about the Digitalmars-d mailing list