digitalmars.D - How to assert a function signature in D2.008?
- Janice Caron (30/30) Dec 04 2007 This used to compile under D2.007
- Christopher Wright (5/44) Dec 04 2007 static assert (is (typeof(&f) == typeof(delegate void (int, char, float,...
- Walter Bright (3/14) Dec 04 2007 Change it to:
- Sean Kelly (4/20) Dec 04 2007 I guess the reason that this matches "void function" rather than "void
- Craig Black (7/27) Dec 05 2007 To me this is confusing because you are taking the address of an instanc...
- Steven Schveighoffer (7/37) Dec 05 2007 It is consistent with other uses of typeof. typeof is a special compile...
- Sean Kelly (5/37) Dec 05 2007 So because &f doesn't involve an instance, the compiler just makes it a
- Steven Schveighoffer (4/24) Dec 05 2007 I was responding to Craig's confusion as to using typeof with an invalid...
- Craig Black (3/43) Dec 05 2007 Ok, that makes sense.
This used to compile under D2.007 struct A { void f() {} static assert(is(typeof(f)==void function())); } It doesn't under D2.008. The only way I've found to make it compile under D2.008 is to change it to struct A { void f() {} static assert(is(typeof(f)==function)); } which means I am no longer able to check the function signature. So, the question is, /how/ do I check the function signature in D2.008? I know I could use ReturnType! and ParameterTypeTuple!, but that would be so tedious for functions with more complex signatures. For example: struct A { int f(int,char,float,int) {} static assert(is(typeof(f)==function)); static assert(is(ReturnType!(f) == int)); static assert(ParameterTypeTuple!(f).length == 4); static assert(is(ParameterTypeTuple!(f)[0] == int)); static assert(is(ParameterTypeTuple!(f)[1] == char)); static assert(is(ParameterTypeTuple!(f)[2] == float)); static assert(is(ParameterTypeTuple!(f)[3] == int)); } Is this really the "right" way to do things now? The old (D2.007) way let me make that check in a single line.
Dec 04 2007
Janice Caron wrote:This used to compile under D2.007 struct A { void f() {} static assert(is(typeof(f)==void function())); } It doesn't under D2.008. The only way I've found to make it compile under D2.008 is to change it to struct A { void f() {} static assert(is(typeof(f)==function)); } which means I am no longer able to check the function signature. So, the question is, /how/ do I check the function signature in D2.008? I know I could use ReturnType! and ParameterTypeTuple!, but that would be so tedious for functions with more complex signatures. For example: struct A { int f(int,char,float,int) {} static assert(is(typeof(f)==function)); static assert(is(ReturnType!(f) == int)); static assert(ParameterTypeTuple!(f).length == 4); static assert(is(ParameterTypeTuple!(f)[0] == int)); static assert(is(ParameterTypeTuple!(f)[1] == char)); static assert(is(ParameterTypeTuple!(f)[2] == float)); static assert(is(ParameterTypeTuple!(f)[3] == int)); } Is this really the "right" way to do things now? The old (D2.007) way let me make that check in a single line.static assert (is (typeof(&f) == typeof(delegate void (int, char, float, int){}))); I don't know how to write the type of a delegate so that it can be used in an is expression.
Dec 04 2007
Janice Caron wrote:This used to compile under D2.007 struct A { void f() {} static assert(is(typeof(f)==void function())); } It doesn't under D2.008. The only way I've found to make it compile under D2.008 is to change it toChange it to: is(typeof(&f)==void function())
Dec 04 2007
Walter Bright wrote:Janice Caron wrote:I guess the reason that this matches "void function" rather than "void delegate" is to avoid the need for handling each separately? SeanThis used to compile under D2.007 struct A { void f() {} static assert(is(typeof(f)==void function())); } It doesn't under D2.008. The only way I've found to make it compile under D2.008 is to change it toChange it to: is(typeof(&f)==void function())
Dec 04 2007
"Sean Kelly" <sean f4.ca> wrote in message news:fj4e63$1jaa$1 digitalmars.com...Walter Bright wrote:To me this is confusing because you are taking the address of an instance function without specifying the context pointer. It doesn't result in a delegate but it shouldn't result in a function either. Maybe there should be another type for "delegate without context pointer". -CraigJanice Caron wrote:I guess the reason that this matches "void function" rather than "void delegate" is to avoid the need for handling each separately? SeanThis used to compile under D2.007 struct A { void f() {} static assert(is(typeof(f)==void function())); } It doesn't under D2.008. The only way I've found to make it compile under D2.008 is to change it toChange it to: is(typeof(&f)==void function())
Dec 05 2007
"Craig Black" wrote"Sean Kelly" <sean f4.ca> wrote in message news:fj4e63$1jaa$1 digitalmars.com...It is consistent with other uses of typeof. typeof is a special compiler directive that says "give me the type of what this instruction would be". It doesn't actually execute the instruction. And I believe the 'is' operation must be executed by the compiler at compile time anyways. -SteveWalter Bright wrote:To me this is confusing because you are taking the address of an instance function without specifying the context pointer. It doesn't result in a delegate but it shouldn't result in a function either. Maybe there should be another type for "delegate without context pointer". -CraigJanice Caron wrote:I guess the reason that this matches "void function" rather than "void delegate" is to avoid the need for handling each separately? SeanThis used to compile under D2.007 struct A { void f() {} static assert(is(typeof(f)==void function())); } It doesn't under D2.008. The only way I've found to make it compile under D2.008 is to change it toChange it to: is(typeof(&f)==void function())
Dec 05 2007
Steven Schveighoffer wrote:"Craig Black" wroteSo because &f doesn't involve an instance, the compiler just makes it a function pointer rather than a delegate, regardless of whether the function is static? I suppose that makes sense. Sean"Sean Kelly" <sean f4.ca> wrote in message news:fj4e63$1jaa$1 digitalmars.com...It is consistent with other uses of typeof. typeof is a special compiler directive that says "give me the type of what this instruction would be". It doesn't actually execute the instruction.Walter Bright wrote:To me this is confusing because you are taking the address of an instance function without specifying the context pointer. It doesn't result in a delegate but it shouldn't result in a function either. Maybe there should be another type for "delegate without context pointer".Janice Caron wrote:I guess the reason that this matches "void function" rather than "void delegate" is to avoid the need for handling each separately? SeanThis used to compile under D2.007 struct A { void f() {} static assert(is(typeof(f)==void function())); } It doesn't under D2.008. The only way I've found to make it compile under D2.008 is to change it toChange it to: is(typeof(&f)==void function())
Dec 05 2007
"Sean Kelly" wroteSteven Schveighoffer wrote:I was responding to Craig's confusion as to using typeof with an invalid instance :) I, like you, think it should be delegate instead... -Steve"Craig Black" wroteSo because &f doesn't involve an instance, the compiler just makes it a function pointer rather than a delegate, regardless of whether the function is static? I suppose that makes sense."Sean Kelly" wrote in messageIt is consistent with other uses of typeof. typeof is a special compiler directive that says "give me the type of what this instruction would be". It doesn't actually execute the instruction.I guess the reason that this matches "void function" rather than "void delegate" is to avoid the need for handling each separately? SeanTo me this is confusing because you are taking the address of an instance function without specifying the context pointer. It doesn't result in a delegate but it shouldn't result in a function either. Maybe there should be another type for "delegate without context pointer".
Dec 05 2007
"Steven Schveighoffer" <schveiguy yahoo.com> wrote in message news:fj6par$2gkq$1 digitalmars.com..."Craig Black" wroteOk, that makes sense."Sean Kelly" <sean f4.ca> wrote in message news:fj4e63$1jaa$1 digitalmars.com...It is consistent with other uses of typeof. typeof is a special compiler directive that says "give me the type of what this instruction would be". It doesn't actually execute the instruction. And I believe the 'is' operation must be executed by the compiler at compile time anyways. -SteveWalter Bright wrote:To me this is confusing because you are taking the address of an instance function without specifying the context pointer. It doesn't result in a delegate but it shouldn't result in a function either. Maybe there should be another type for "delegate without context pointer". -CraigJanice Caron wrote:I guess the reason that this matches "void function" rather than "void delegate" is to avoid the need for handling each separately? SeanThis used to compile under D2.007 struct A { void f() {} static assert(is(typeof(f)==void function())); } It doesn't under D2.008. The only way I've found to make it compile under D2.008 is to change it toChange it to: is(typeof(&f)==void function())
Dec 05 2007