Parametrized function type with argument void

39 views
Skip to first unread message

Jan Volf

unread,
Jul 6, 2013, 9:47:10 AM7/6/13
to haxe...@googlegroups.com
Hi folks,
I'm having a problem with parametrized function type where argument type is set to a Void. Here's example:

class Function<A,R> {


    private var callback:A->R;


    public function new(callback:A->R) {


        this.callback = callback;


    }


    public function call(arg:A):R {


        return this.callback(arg);


    }


}


class Test1


{


    static function main()


    {


        var f1:Function<Int,String> = new Function<Int,String>(function(a:Int) {


            return Std.string(a);


        });


        var f2:Function<Void,String> = new Function<Void,String>(function() { // Void -> String should be (Void) -> String


            return "ok";


        });


        var f3:Function<Int,String> = new Function<Int,String>(function() { // Void -> String should be Int -> String


            return "ok";


        });


    }


}

 The problem is that  with the definition of the f2 variable where the compiler complains at the argument to the Function class constructor: Void -> String should be (Void) -> String
Why the is the Void type is in parenthesis? I suppose that the parenthesis are the reason why it does not compile at this point. If I try another type instead of the Void compiler gives expectable result (see f3 definition).
Is there another way how to provide empty parameter type to parametrized class/function by type erasure like this instead of using Void type?

Regards
Jan

Luca

unread,
Jul 6, 2013, 7:01:20 PM7/6/13
to haxe...@googlegroups.com
Well the problem is clearly that Void cannot make sense as either type param.

In the first case, it would make a function argument be typed with Void, which makes no sense. and in the latter, it would cause a function return to be typed Void, where that function return value is used (to be returned) which makes no sense.

Jan Volf

unread,
Jul 7, 2013, 4:23:25 AM7/7/13
to haxe...@googlegroups.com
Thanks fro response,
Why the Void makes no sense as type parameter? It also works in other use cases, i.e. function return type.
Also the function argument type Void makes sense, when passing callbacks as function arguments just telling the compiler that the callback is expected to have no argument:

class Test {


    public static function main() {


        callLater(function() {


            trace("ok");


        });


    }


   


    public static function callLater(callback:Void->Void) {


        // whatever


    }


}

This compiles as expected.
The problem arises apparently only if the function type argument is parametrized and thru this parameter it is set to Void but compiler mysteriously expects (Void) - notice the parenthesis.
The third case in original example was just the test of other type than Void as parametrized function type argument to see that for other types the compiler expects that type without parenthesis.

Jan

Luca

unread,
Jul 7, 2013, 6:48:33 AM7/7/13
to haxe...@googlegroups.com
In your code from first post:


public function call(arg:A):R return this.callback(arg);

becomes with A = Void

public function call(arg:Void):R return this.callback(arg);

which is invalid, you cannot have an argument of type Void

with R = Void:

public function call(arg:A):Void return this.callback(arg);

you cannot return a value if the function is typed X->Void, furthermore callback itself will have type A->Void meaning it has no return value, but you try and forward that return value, which again would be invalid.

Reply all
Reply to author
Forward
0 new messages