How to declare an alias to a function literal type
ParticlePeter via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Tue Jan 12 09:32:16 PST 2016
On Tuesday, 12 January 2016 at 17:03:49 UTC, Ali Çehreli wrote:
> On 01/12/2016 08:55 AM, ParticlePeter wrote:
>
> > I have a function "otherFunc" which takes a function with
> lots of
> > parameters as argument:
> >
> > void otherFunc( void function( ref int p1, float p2, ubyte
> p3, ... ) mf );
>
> Ok.
>
> > otherFunc( void function( ref int p1, float p2, ubyte p3 ) {
> myCode; } );
>
> Ok.
>
> > alias MF = void function( ref int p1, float p2, ubyte p3 );
>
> Ok.
>
> > I can rewrite the definition of otherFunc like this:
> > void otherFunc( MF mf );
>
> That has the same problem of trying to do this for int:
>
> void foo(int i) {
> }
>
> void main() {
> foo(int 42); // <-- ERROR
> }
>
> But you can do this:
>
> foo(int(42)); // (Relatively new syntax in D.)
>
> > But I cannot pass an anonymous function to otherFunc like
> this:
> > otherFunc( MF { myCode; } );
>
> It works with the parentheses as it does for int:
>
> alias MF = void function( ref int p1, float p2, ubyte p3 );
>
> void otherFunc( MF mf ) {
> }
>
> void main() {
> otherFunc(MF((ref int, float, ubyte){ })); // <-- Parens
> }
O.K. so I conclude that writing:
void main() {
otherFunc(MF { });
}
is not possible. At least not with alias, maybe with templates or
mixins?
In essence something like C #define as in:
#define MF function( ref int p1, float p2, ubyte p3 )
Is there some such way?
More information about the Digitalmars-d-learn
mailing list