www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - How to assert a function signature in D2.008?

reply "Janice Caron" <caron800 googlemail.com> writes:
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
next sibling parent Christopher Wright <dhasenan gmail.com> writes:
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
prev sibling parent reply Walter Bright <newshound1 digitalmars.com> writes:
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
Change it to: is(typeof(&f)==void function())
Dec 04 2007
parent reply Sean Kelly <sean f4.ca> writes:
Walter Bright wrote:
 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
Change it to: is(typeof(&f)==void function())
I guess the reason that this matches "void function" rather than "void delegate" is to avoid the need for handling each separately? Sean
Dec 04 2007
parent reply "Craig Black" <cblack ara.com> writes:
"Sean Kelly" <sean f4.ca> wrote in message 
news:fj4e63$1jaa$1 digitalmars.com...
 Walter Bright wrote:
 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
Change it to: is(typeof(&f)==void function())
I guess the reason that this matches "void function" rather than "void delegate" is to avoid the need for handling each separately? Sean
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". -Craig
Dec 05 2007
parent reply "Steven Schveighoffer" <schveiguy yahoo.com> writes:
"Craig Black" wrote
 "Sean Kelly" <sean f4.ca> wrote in message 
 news:fj4e63$1jaa$1 digitalmars.com...
 Walter Bright wrote:
 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
Change it to: is(typeof(&f)==void function())
I guess the reason that this matches "void function" rather than "void delegate" is to avoid the need for handling each separately? Sean
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". -Craig
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. -Steve
Dec 05 2007
next sibling parent reply Sean Kelly <sean f4.ca> writes:
Steven Schveighoffer wrote:
 "Craig Black" wrote
 "Sean Kelly" <sean f4.ca> wrote in message 
 news:fj4e63$1jaa$1 digitalmars.com...
 Walter Bright wrote:
 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
Change it to: is(typeof(&f)==void function())
I guess the reason that this matches "void function" rather than "void delegate" is to avoid the need for handling each separately? Sean
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".
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.
So 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
Dec 05 2007
parent "Steven Schveighoffer" <schveiguy yahoo.com> writes:
"Sean Kelly" wrote
 Steven Schveighoffer wrote:
 "Craig Black" wrote
 "Sean Kelly" wrote in message
 I guess the reason that this matches "void function" rather than "void 
 delegate" is to avoid the need for handling each separately?


 Sean
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".
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.
So 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.
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
Dec 05 2007
prev sibling parent "Craig Black" <cblack ara.com> writes:
"Steven Schveighoffer" <schveiguy yahoo.com> wrote in message 
news:fj6par$2gkq$1 digitalmars.com...
 "Craig Black" wrote
 "Sean Kelly" <sean f4.ca> wrote in message 
 news:fj4e63$1jaa$1 digitalmars.com...
 Walter Bright wrote:
 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
Change it to: is(typeof(&f)==void function())
I guess the reason that this matches "void function" rather than "void delegate" is to avoid the need for handling each separately? Sean
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". -Craig
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. -Steve
Ok, that makes sense.
Dec 05 2007