Thread overview | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
|
September 21, 2012 function is not function | ||||
---|---|---|---|---|
| ||||
solution is to use std.traits, but can someone explain this to me? import std.stdio; void main() { auto a = { writeln("hi"); }; pragma(msg, typeof(a)); // void function() pragma(msg, is(typeof(a) == delegate)); // nope! pragma(msg, is(typeof(a) == function)); // nope! } |
September 21, 2012 Re: function is not function | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ellery Newcomer | On 09/21/2012 12:59 PM, Ellery Newcomer wrote: > solution is to use std.traits, but can someone explain this to me? > > import std.stdio; > > void main() { > auto a = { > writeln("hi"); > }; > pragma(msg, typeof(a)); // void function() > pragma(msg, is(typeof(a) == delegate)); // nope! > pragma(msg, is(typeof(a) == function)); // nope! > } You have probably tried the following already: pragma(msg, is(typeof(a) == void function())); Regardless, I think it is a bug because the documentation says that the 'function' keyword alone should work: http://dlang.org/expression.html#IsExpression <quote> is ( Type == TypeSpecialization ) The condition is satisfied if Type is semantically correct and is the same type as TypeSpecialization. If TypeSpecialization is one of struct union class interface enum function delegate const immutable shared then the condition is satisifed if Type is one of those. </quote> Ali |
September 21, 2012 Re: function is not function | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ellery Newcomer | Ellery Newcomer:
> import std.stdio;
>
> void main() {
> auto a = {
> writeln("hi");
> };
> pragma(msg, typeof(a)); // void function()
> pragma(msg, is(typeof(a) == delegate)); // nope!
> pragma(msg, is(typeof(a) == function)); // nope!
> }
There is a subtle difference between function type, type of
function pointer and delegate type.
int foo1() { return 0; }
void main() {
static assert(is(typeof(foo1) == function));
auto foo2 = { return 0; };
static assert(is(typeof(*foo2) == function));
}
Bye,
bearophile
|
September 21, 2012 Re: function is not function | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ali Çehreli | Ellery Newcomer:
>> pragma(msg, typeof(a)); // void function()
Maybe pragma(msg) needs to print function pointer types with a "*", to help remember it's a pointer:
int foo1() pure nothrow @safe { return 0; }
void main() {
static assert(is(typeof(foo1) == function));
auto foo2 = { return 0; };
static assert(is(typeof(*foo2) == function));
pragma(msg, typeof(foo1));
pragma(msg, typeof(foo2));
}
Prints:
pure nothrow @safe int()
int function() pure nothrow @safe
From such printout I am not able to tell the first one is a function type and the second is the type of a function pointer :-(
Bye,
bearophile
|
September 21, 2012 Re: function is not function | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ali Çehreli | On 09/21/2012 01:10 PM, Ali Çehreli wrote:
>
> You have probably tried the following already:
>
> pragma(msg, is(typeof(a) == void function()));
>
No, but that's also not very generic.
void main() {
auto a = {
return 1;
};
pragma(msg, is(typeof(a) == void function())); // nope!
pragma(msg, typeof(a)); // void function() pure nothrow @safe
}
guess what I'm fighting with just now
|
September 21, 2012 Re: function is not function | ||||
---|---|---|---|---|
| ||||
Posted in reply to bearophile | On 09/21/2012 01:17 PM, bearophile wrote:
>> pragma(msg, is(typeof(a) == function)); // nope!
code in pyd suggests this evaluated to true once upon a time.
|
September 22, 2012 Re: function is not function | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ellery Newcomer | On 09/21/2012 10:41 PM, Ellery Newcomer wrote:
> On 09/21/2012 01:17 PM, bearophile wrote:
>>> pragma(msg, is(typeof(a) == function)); // nope!
>
> code in pyd suggests this evaluated to true once upon a time.
>
I don't think it ever did. It is just very easy to get wrong.
|
September 22, 2012 Re: function is not function | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ellery Newcomer | On Friday, September 21, 2012 12:59:31 Ellery Newcomer wrote: > solution is to use std.traits, but can someone explain this to me? > > import std.stdio; > > void main() { > auto a = { > writeln("hi"); > }; > pragma(msg, typeof(a)); // void function() > pragma(msg, is(typeof(a) == delegate)); // nope! > pragma(msg, is(typeof(a) == function)); // nope! > } Sorry if this ends up being a double-post, but the post I made hours ago doesn't seem to be showing up, so I'm posting it again: http://stackoverflow.com/questions/11067972 - Jonathan M Davis |
September 26, 2012 Re: function is not function | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ellery Newcomer | On 21/09/12 21:59, Ellery Newcomer wrote:
> solution is to use std.traits, but can someone explain this to me?
>
> import std.stdio;
>
> void main() {
> auto a = {
> writeln("hi");
> };
> pragma(msg, typeof(a)); // void function()
> pragma(msg, is(typeof(a) == delegate)); // nope!
> pragma(msg, is(typeof(a) == function)); // nope!
> }
The 'function' keyword is an ugly wart in the language. In
void function()
'function' means 'function pointer'. But in
is (X == function)
'function' means 'function'.
Which is actually pretty much useless. You always want 'function pointer'. This is the only case where "function type" still exists in the language.
|
Copyright © 1999-2021 by the D Language Foundation