www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - function is not function

reply Ellery Newcomer <ellery-newcomer utulsa.edu> writes:
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!
}
Sep 21 2012
next sibling parent reply =?UTF-8?B?QWxpIMOHZWhyZWxp?= <acehreli yahoo.com> writes:
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
Sep 21 2012
next sibling parent "bearophile" <bearophileHUGS lycos.com> writes:
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
Sep 21 2012
prev sibling parent Ellery Newcomer <ellery-newcomer utulsa.edu> writes:
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
Sep 21 2012
prev sibling next sibling parent reply "bearophile" <bearophileHUGS lycos.com> writes:
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
Sep 21 2012
parent reply Ellery Newcomer <ellery-newcomer utulsa.edu> writes:
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.
Sep 21 2012
parent Timon Gehr <timon.gehr gmx.ch> writes:
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.
Sep 21 2012
prev sibling next sibling parent "Jonathan M Davis" <jmdavisProg gmx.com> writes:
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
Sep 21 2012
prev sibling parent Don Clugston <dac nospam.com> writes:
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.
Sep 26 2012