Thread overview | ||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
May 21, 2011 What is this strange alias syntax? | ||||
---|---|---|---|---|
| ||||
Taken from the docs: alias int func(int); void main() { if ( is(func[]) ) // not satisfied because arrays of writeln("satisfied"); // functions are not allowed else writeln("not satisfied"); } It will print not satisfied. But I'm not sure what func is supposed to be? An alias.. to what? I can't declare variables of its type: func x; error: variable test.main.x cannot be declared to be a function Of course if you write the alias the usual way it will print 'satisfied'. Nothing strange about having an array of functions in D: alias int function(int) func; void main() { if ( is(func[]) ) writeln("satisfied"); else writeln("not satisfied"); } |
May 21, 2011 Re: What is this strange alias syntax? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Andrej Mitrovic | Andrej Mitrovic Wrote:
> Taken from the docs:
>
> alias int func(int);
> void main()
> {
> if ( is(func[]) ) // not satisfied because arrays of
> writeln("satisfied"); // functions are not allowed
> else
> writeln("not satisfied");
> }
>
> It will print not satisfied. But I'm not sure what func is supposed to be? An alias.. to what? I can't declare variables of its type:
> func x;
> error: variable test.main.x cannot be declared to be a function
>
> Of course if you write the alias the usual way it will print 'satisfied'. Nothing strange about having an array of functions in D:
>
> alias int function(int) func;
> void main()
> {
> if ( is(func[]) )
> writeln("satisfied");
> else
> writeln("not satisfied");
> }
Well, I have no idea. File a bug maybe.
|
May 21, 2011 Re: What is this strange alias syntax? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Andrej Mitrovic | On Sat, 21 May 2011 05:12:20 +0200, Andrej Mitrovic <none@none.none> wrote: > Taken from the docs: > > alias int func(int); > void main() > { > if ( is(func[]) ) // not satisfied because arrays of > writeln("satisfied"); // functions are not allowed > else > writeln("not satisfied"); > } > > It will print not satisfied. But I'm not sure what func is supposed to be? An alias.. to what? I can't declare variables of its type: > func x; > error: variable test.main.x cannot be declared to be a function > > Of course if you write the alias the usual way it will print 'satisfied'. Nothing strange about having an array of functions in D: > > alias int function(int) func; > void main() > { > if ( is(func[]) ) > writeln("satisfied"); > else > writeln("not satisfied"); > } It's the old C syntax for defining function pointers. Well, without the pointer. And that last part is important, because the latter example is an array of function pointers, with which D has no issue. -- Simen |
May 22, 2011 Re: What is this strange alias syntax? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Simen Kjaeraas | On Sat, 21 May 2011 05:15:32 -0400, Simen Kjaeraas <simen.kjaras@gmail.com> wrote:
> On Sat, 21 May 2011 05:12:20 +0200, Andrej Mitrovic <none@none.none> wrote:
>
>> Taken from the docs:
>>
>> alias int func(int);
>> void main()
>> {
>> if ( is(func[]) ) // not satisfied because arrays of
>> writeln("satisfied"); // functions are not allowed
>> else
>> writeln("not satisfied");
>> }
>>
>> It will print not satisfied. But I'm not sure what func is supposed to be? An alias.. to what? I can't declare variables of its type:
>> func x;
>> error: variable test.main.x cannot be declared to be a function
>>
>> Of course if you write the alias the usual way it will print 'satisfied'. Nothing strange about having an array of functions in D:
>>
>> alias int function(int) func;
>> void main()
>> {
>> if ( is(func[]) )
>> writeln("satisfied");
>> else
>> writeln("not satisfied");
>> }
>
> It's the old C syntax for defining function pointers. Well, without the
> pointer. And that last part is important, because the latter example is
> an array of function pointers, with which D has no issue.
>
Yes, and I thought we were killing that syntax because of how horrible it is?
-Steve
|
May 22, 2011 Re: What is this strange alias syntax? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Simen Kjaeraas | On 21/05/2011 10:15, Simen Kjaeraas wrote: > On Sat, 21 May 2011 05:12:20 +0200, Andrej Mitrovic <none@none.none> wrote: > >> Taken from the docs: >> >> alias int func(int); <snip> > It's the old C syntax for defining function pointers. <snip> Functions, not function pointers. The C syntax for declaring function pointers is int (*func)(int); In C (with "typedef" instead of "alias"), what you're effectively doing is declaring a type that you can't use directly, but can declare pointers to. But why does this exist in D? Stewart. |
May 22, 2011 Re: What is this strange alias syntax? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Steven Schveighoffer | On 22/05/2011 11:57, Steven Schveighoffer wrote: > On Sat, 21 May 2011 05:15:32 -0400, Simen Kjaeraas <simen.kjaras@gmail.com> wrote: <snip> >> It's the old C syntax for defining function pointers. Well, without the >> pointer. And that last part is important, because the latter example is >> an array of function pointers, with which D has no issue. > > Yes, and I thought we were killing that syntax because of how horrible it is? Indeed, that D has this notion of immediate function types is absurd: - it's mostly undocumented; indeed, no mention of it on type.html - there's no native D syntax for it, only the kludgy syntax inherited from C - it seems silly to have a type that you can't declare variables of and that (unlike void) has no obvious practical reason for its existence - it leads to such corner cases as http://d.puremagic.com/issues/show_bug.cgi?id=3650 Stewart. |
May 22, 2011 Re: What is this strange alias syntax? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Stewart Gordon | Should I file a bug report to kill this syntax? |
May 22, 2011 Re: What is this strange alias syntax? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Andrej Mitrovic | 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 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" alias int func(int); //defines func as type "function that takes int and returns int" It is perfectly consistent with other uses of alias. It is the way alias works (taken from typedef), and function declarations work (also taken from c) that are somewhat strange, not this particular case. Timon |
May 22, 2011 Re: What is this strange alias syntax? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Timon Gehr | 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". > 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. > 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; } Stewart. |
May 22, 2011 Re: What is this strange alias syntax? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Stewart Gordon | > 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 |
Copyright © 1999-2021 by the D Language Foundation