Need a function of type (Void) -> Void

178 views
Skip to first unread message

Theodore Norvell

unread,
May 24, 2015, 4:40:04 PM5/24/15
to haxe...@googlegroups.com
I have a function that takes an argument of type (Void) -> Void.  However if I pass in
             function () { trace("hi") ; }
I get an error
             Void -> Void should be (Void) -> Void

And if I pass in a function
              function( x : Void ) { trace("hi") ; }
I get an error
              Arguments and variables of type Void are not allowed.

Is there a way to write a function of type (Void) -> Void ?

Background.  In case you are wondering how I ended up needing a function that takes a (Void) -> Void, here is how. (This is not my actual application, but a simpler version.)  I have a generic function
             static function go<A,B>( f : B -> A, g : A -> Void, b : B) : Void { g(f(b)) ; }
Now I call it thus
             go( function( i : Int ) : Void {  },   function( x : Void ) { trace("hi") ; }, 41 )

Also: Some of the results I'm getting seem to vary according to target. Are there target dependent differences in how Void is treated?

Matthew Spencer

unread,
May 24, 2015, 4:56:47 PM5/24/15
to haxe...@googlegroups.com
There is no way to write a (Void) -> Void function.  Use can however use a Unit type.



For an explanation of Void vs Unit read:

Theodore Norvell

unread,
May 24, 2015, 10:07:18 PM5/24/15
to haxe...@googlegroups.com
Thanks Matthew.  That fully answers my question.

The Haxe manual says that the only value of type Void is null and furthermore
       function f() : Void { return null ; }
compiles and seems to be equivalent to
      function f() : Void { }
So I had hoped that Void could act as a Unit type.

I think I shall use
      enum Triv {} ;
and put up with the extra "return null ;" when it is needed.
Reply all
Reply to author
Forward
0 new messages