What is this strange alias syntax?
Timon Gehr
timon.gehr at gmx.ch
Sun May 22 13:51:14 PDT 2011
> On 22/05/2011 16:20, Timon Gehr wrote:
>> Andrej Mitrovic wrote:
>>> Should I file a bug report to kill this syntax?
>>
>> No. It is perfectly valid, see grammar:
>> http://www.digitalmars.com/d/2.0/declaration.html
>
> Grammar states only that it's syntactically valid, and makes no comment on
semantic validity.
>
> I suspect what Andrej actually meant is to kill treating function signatures as
types in
> this way. And possibly "enhancement request" rather than "bug".
It is the opposite of an enhancement request. It means removing a feature that
cannot be meaningfully substituted by other features. (also, removing it increases
the inconsistency of the language and makes the compiler more complex).
>
>> What is strange about this syntax in particular?
>>
>> int i; //declares i of type "int"
>> alias int i; //defines i as type "int"
>>
>> int func(int); //declares func of type "function that takes int and returns int"
>
> True, if you use "type" in a broad sense. But it doesn't declare a variable of
that type,
> and you can't use it as a type as such. Really, function signatures and data
types are
> distinct entities and ought not to be interchangeable.
You can alias any symbol. Data types and other symbols are distinct entities too.
By your arguments, this is a bad thing to have.
Also, a function signature, while not a data type is still a _type_ (the "broad
sense" is the D sense. You can do typeof(some_function)).
>
>> alias int func(int); //defines func as type "function that takes int and
returns int"
>>
>> It is perfectly consistent with other uses of alias.
><snip>
>
> I wouldn't say consistent. The essence of an alias is that you can use it where
you would
> use what it's an alias of. But in that case, you should be able to do:
>
> alias int func();
>
> func main {
> return 0;
> }
>
This does not make sense. The problem is, that currently there is no other way
than alias to even _refer_ to a function signature.
alias int func();
int main(){
static assert(is(typeof(main)==func);
return 0;
}
I do not get why anybody would want to remove the possibility to refer to function
signatures. It can be handy for template constraining. I agree that the way to do
it is somewhat strange, but it is easy for the compiler to handle.
> Stewart.
What I consider strange is that:
import std.stdio;
int main(){
writeln(typeof(main).stringof);
}
Will print "int()()", which is still the C way of referring to function types. In
D, this looks more like the type of a function template.
It gets even more strange:
alias int func();
template tt(func a){
enum tt=a.stringof;
}
Error: variable ttt.a cannot be declared to be a function
Error: arithmetic/string type expected for value-parameter, not int()
Note how it is only "int()" now.
I think C function pointer syntax was maybe deprecated too early, because all
other function type-related stuff is still very near to how C does it. This should
be solved. But I cannot think of any reasonable solution that would not break easy
parsing. Any ideas?
Timon
More information about the Digitalmars-d-learn
mailing list