Default values for optional variables in typedefs?

1,124 views
Skip to first unread message

Will Maynard

unread,
Sep 10, 2015, 7:42:59 PM9/10/15
to Haxe
Is there a way to assign a default value to an optional variable in a typedef?

I'm hoping I'm just missing something simple.

Here's a vague example of what I'd like to do:

class MyGuiObj {
   
public function new(dim:Dimensions) {
       
this.x = dim.x;
       
this.y = dim.y;
       
//...
   
}
}

typedef Dimensions = {
   
@:optional var x:Float = 0;
   
@:optional var y:Float = 0;
   
@:optional var w:Float = 100;
   
@:optional var h:Float = 30;
}

Unfortunately, this results in an error ("Expression not allowed here").

As it is, I need to handle each of the typedef's optional values as a null first:

    public function new(dim:Dimensions) {
       
if (dim.x == null)
            dim
.x = 0;
       
if (dim.y == null)
            dim
.y = 0;
       
if (dim.w == null)
            dim
.w = 0;
       
if (dim.h == null)
            dim
.h = 0;
       
this.x = dim.x;
       
this.y = dim.y;
       
//...
   
}

This really bulks up the code significantly as I have to do this in every situation where I use optional values with my typedefs.  While not a worry for me (yet), I would also imagine the branching of checking values so often affects performance, whereas a default value would prevent that.

Dan Korostelev

unread,
Sep 11, 2015, 8:00:18 AM9/11/15
to Haxe
Firstly, typedef is just an alias for a type, so you probably mean a structure type. The thing is, it's mostly a compile-time definition and it can unify with any run-time type that provides required fields, so default values makes no sense here. You could just use a class here:

@:publicFields
class Dimensions {
    var x:Float = 0;
    var y:Float = 0;
    var w:Float = 100;
    var h:Float = 30;
    function new() {}
}

пятница, 11 сентября 2015 г., 2:42:59 UTC+3 пользователь Will Maynard написал:

Will Maynard

unread,
Sep 11, 2015, 1:23:17 PM9/11/15
to Haxe
While this would work for the default values, this would require me to pass in a Dimensions class object and I wouldn't have the named parameters of a typedef (new MyGuiObj({x:500, y:40});).

For now, I'll just create a static function somewhere to set default values to my typedefs to keep the above functionality, which is a higher priority for my project.

I might just be too used to C#'s named parameters.  I wish the recent feature request for it wasn't shut down as quickly as it was - typedefs just aren't as versatile.

Dan Korostelev

unread,
Sep 11, 2015, 1:39:16 PM9/11/15
to Haxe
While I agree that named arguments would be useful, there are some libraries to help with class object initialization, for example:

https://github.com/jasononeil/objectinit
https://github.com/ciscoheat/dataclass

пятница, 11 сентября 2015 г., 20:23:17 UTC+3 пользователь Will Maynard написал:
Message has been deleted

Max

unread,
Sep 11, 2015, 5:49:07 PM9/11/15
to Haxe
Nicolas proposed something like

@:struct class Dimensions {
 
public var x:Float;
 
public var y:Float;
  public var w:Float;
 
public var h:Float;
 
public inline new(?x=0,?y=0,?w=100,?h=30) {
     
this.x = x;
     
this.y = y;
      this.w = w;
     
this.h = h;
  }
}
// would allow to be initialized with:
var m1 : Dimensions = { x : 500, y : 40 };
var m2 : Dimensions = { x : 0, y : 0, w : 200, h : 100 };


here

https://github.com/HaxeFoundation/haxe/issues/4526
Reply all
Reply to author
Forward
0 new messages