Default value of a generic type.

302 views
Skip to first unread message

Herp Derp

unread,
May 1, 2014, 6:49:22 AM5/1/14
to haxe...@googlegroups.com
I have a generic class with the @:generic metadata, and under certain circumstances I would like to give a field of the generic type its default value back.

A commented example code:

@:generic
class MyClass<T> {
    public var value: T;

    public function new(value: T) {
        this.value = value;
    }

    public function resetToDefault(): Void {
        // Set the 'value' field to its default value
        // according to its compile-time type.
        // `0` for Int
        // `0.0` for Float
        // `false` for Bool
        // `null` for anything else
        
        // In C# its possible to do it like so:
        this.value = default(T); // invalid Haxe code
        // Done at compilation time with no
        // performance penalty at runtime.
    }
}

Is there a built-in tool that can accomplish this? perhaps its possible to write a macro that will do this?

Hugh

unread,
May 2, 2014, 12:22:10 AM5/2/14
to haxe...@googlegroups.com

On cpp, you can set them to the null-dynamic:

var i:Int = 123;
var d:Dynamic = null; i = d;
trace(i);

You could set it to null directly, but the compiler wont let you, event with "untyped", but this should work:

  untyped i = __cpp__("null()");

Hugh

Hugh

unread,
May 2, 2014, 12:26:11 AM5/2/14
to haxe...@googlegroups.com

Another option might be to create a defaultValue member (or static, if that works with generics) an let it initialize normally, and never set it.  Then just use "value=defaultValue".

Also note that on neko, the integers will start off at null, not 0.

Hugh

Message has been deleted

Herp Derp

unread,
May 2, 2014, 6:47:58 AM5/2/14
to haxe...@googlegroups.com


On Friday, May 2, 2014 7:22:10 AM UTC+3, Hugh wrote:

On cpp, you can set them to the null-dynamic:

var i:Int = 123;
var d:Dynamic = null; i = d;
trace(i);

You could set it to null directly, but the compiler wont let you, event with "untyped", but this should work:

  untyped i = __cpp__("null()");

I'm not trying to target C++, so this doesn't really help me.
 
On Friday, May 2, 2014 7:26:11 AM UTC+3, Hugh wrote:

Another option might be to create a defaultValue member (or static, if that works with generics) an let it initialize normally, and never set it.  Then just use "value=defaultValue".

Also note that on neko, the integers will start off at null, not 0.


Generic classes can't have static fields, so this won't work. 

Juraj Kirchheim

unread,
May 2, 2014, 7:23:04 AM5/2/14
to haxe...@googlegroups.com
FYI the default value for floats is NaN, not 0.0. In any case, you can
write a macro for that:

class Default {

macro static public function value()
return
switch haxe.macro.Context.follow(haxe.macro.Context.getExpectedType()) {
case TAbstract(t, _):
switch t.toString() {
case 'Int': macro 0;
case 'Bool': macro false;
case 'Float': macro 0.0;
default: macro null;
}
default: macro null;
}
}

//usage:
this.value = Default.value();

Regards,
Juraj
> --
> To post to this group haxe...@googlegroups.com
> http://groups.google.com/group/haxelang?hl=en
> ---
> You received this message because you are subscribed to the Google Groups
> "Haxe" group.
> For more options, visit https://groups.google.com/d/optout.

Stephane Le Dorze

unread,
May 2, 2014, 8:09:14 AM5/2/14
to haxe...@googlegroups.com
Ooooo..
I was not aware of "getExpectedType", that's very good to have it on board! =)

Herp Derp

unread,
May 4, 2014, 9:40:25 AM5/4/14
to haxe...@googlegroups.com
You're right of course about NaN being the default for Float, my mistake.
That is an interesting piece of code, but unfortunately it doesn't work in a generic context.

Apparently it always returns null for a generic type and results in the following error (T is Bool in this case):

On static platforms, null can't be used as basic type Bool

:(

Herp Derp

unread,
May 16, 2014, 1:05:38 AM5/16/14
to haxe...@googlegroups.com
bump

Juraj Kirchheim

unread,
May 19, 2014, 6:12:53 AM5/19/14
to haxe...@googlegroups.com
Can you post a minimal example that reproduces the problem?

On Fri, May 16, 2014 at 7:05 AM, Herp Derp <stupidi...@gmail.com> wrote:
> bump

Daniel Uranga

unread,
May 19, 2014, 4:47:17 PM5/19/14
to haxe...@googlegroups.com
Im interested on this too.
https://gist.github.com/anonymous/915f1948d402676c6b3c is that correct? I get the "On static platforms, null can't be used as basic type Float" error too.

Line 6 "trace(expected)" is printing "TInst(Test.T,[])"; so the switch is defaulting to null.

Juraj Kirchheim

unread,
May 20, 2014, 5:45:41 AM5/20/14
to haxe...@googlegroups.com
Oh, well that's unpleasant. I had hoped macros are executed after
@:generic, which arguably makes more sense. Try filing an issue ;)

Daniel Uranga

unread,
May 20, 2014, 3:36:05 PM5/20/14
to haxe...@googlegroups.com
I had this problem several times trying to write code with @:generic, so it would be really nice to have this feature added.
Reply all
Reply to author
Forward
0 new messages