Method detecting type of argument

78 views
Skip to first unread message

Adrian Martinez

unread,
May 8, 2015, 6:13:44 AM5/8/15
to haxe...@googlegroups.com
Hi all,

I’ve got a method which can accept a String or Float  as value.
In this case I would have to use an Enum.
And then I would have to specify the Type every time it’s used like:

foo.set( Float( value )) or foo.set( String( value )).

But I would prefer to write it like this:

foo.set( value );

Is there a way to write it like this and have the method detect which type it is?

Adrian

Dan Korostelev

unread,
May 8, 2015, 8:44:07 AM5/8/15
to haxe...@googlegroups.com
If i understand you correctly, you can achieve this using abstract casting, like this: http://try.haxe.org/#379dD

пятница, 8 мая 2015 г., 13:13:44 UTC+3 пользователь Adrian Martinez написал:

Adrian Martinez

unread,
May 8, 2015, 4:01:54 PM5/8/15
to haxe...@googlegroups.com
Hi Dan,

Marvellous solution!
This is exactly where I was looking for.
Thank you very much for the example.

Best, Adrian

Op vrijdag 8 mei 2015 14:44:07 UTC+2 schreef Dan Korostelev:

Adrian Martinez

unread,
May 9, 2015, 4:05:20 AM5/9/15
to haxe...@googlegroups.com
Hi Dan,

I would also like to use an Array<Value> as optional argument.
So the possible use cases would be:

foo(1);
foo
("hello");
foo
([1, hello”]);

Is this also possible inside this context?
I tried implementing it in your example but I couldn’t make it work.


Best, Adrian

Op vrijdag 8 mei 2015 14:44:07 UTC+2 schreef Dan Korostelev:
If i understand you correctly, you can achieve this using abstract casting, like this: http://try.haxe.org/#379dD

Mathieu Emilian

unread,
May 9, 2015, 7:33:28 AM5/9/15
to haxe...@googlegroups.com

Hi,
Alternatively, you could use the Either abstract like this http://try.haxe.org/#7f692

Adrian Martinez

unread,
May 15, 2015, 11:04:13 AM5/15/15
to haxe...@googlegroups.com
Thanks Mathieu, works great!
Enough possibilities to choose from.

Adrian

Op zaterdag 9 mei 2015 13:33:28 UTC+2 schreef Mathieu Emilian:
Reply all
Reply to author
Forward
0 new messages