1: Detection of an array type? 2: Constraints type of function parameter?

71 views
Skip to first unread message

Tom

unread,
Aug 28, 2014, 4:49:24 AM8/28/14
to haxe...@googlegroups.com
Hi!

1.: There is a method to detect a type of an array, if it is typed? Like var as:Array<String> = [];.
trace(Type.typeof(as));

Trace out: TClass(#Array), nothing more.

2.: I need a function, which has 2 parameters:

function test(obj:Dynamic, params:Dynamic) { }

The params variable can be constrained to String, or Array<String>?
The manual doesn't help me out. :-/

Thanks in advance!
Tom

Simon Krajewski

unread,
Aug 28, 2014, 5:00:29 AM8/28/14
to haxe...@googlegroups.com


On 28 Aug 2014 10:49, "Tom" <hortoba...@gmail.com> wrote:
>
> Hi!
>
> 1.: There is a method to detect a type of an array, if it is typed? Like var as:Array<String> = [];.
> trace(Type.typeof(as));
>
> Trace out: TClass(#Array), nothing more.

There's no cross-target representation of runtime type parameters. This should be obvious if you're looking at targets like Javascript where you cannot tell what kind of elements `[]` has.

If your array has an element you can just typeof that.

> 2.: I need a function, which has 2 parameters:
>
> function test(obj:Dynamic, params:Dynamic) { }
>
> The params variable can be constrained to String, or Array<String>?
> The manual doesn't help me out. :-/

The real question is how you would use a thusly constrained type. The only thing they have in common is the length field, so you could use `{length:Int}` instead of Dynamic.

There are tricks using abstracts here, but this then requires runtime switching on something in order to figure out what kind of value you are dealing with.

Simon

Tom

unread,
Aug 28, 2014, 5:10:40 AM8/28/14
to haxe...@googlegroups.com

I only want to allow these 2 types, that already the compiler throws error, if the type mismatch.
I can figure out in runtime, which params was passed, by the Std.is(params, String) code.

Tom

Tom

unread,
Aug 29, 2014, 10:17:01 AM8/29/14
to haxe...@googlegroups.com
Nobody know any solution to detect an array type, and/or contraint a function second parameter to certain types?

Simon Krajewski

unread,
Aug 29, 2014, 10:23:15 AM8/29/14
to haxe...@googlegroups.com

The whole idea is so horrible you might as well use Dynamic.

Simon

--
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.

Gama11

unread,
Aug 29, 2014, 10:46:17 AM8/29/14
to haxe...@googlegroups.com
It is possible to use an abtract for this "either or" kinda thing, but the underyling type will still be Dynamic, so use with care, if at all.

class Test {
    static function main() {
        stringOrArrayString("Test");
        stringOrArrayString(["T", "e", "s", "t"]);
        
        // does not compile
        //stringOrArrayString(1);
    }
    
    static function stringOrArrayString(s:Either<String, Array<String>>) {
        if (Std.is(s, String)) {
            trace("String");
        } else {
            trace("String Array");
        }
    }
}

abstract Either<T1, T2>(Dynamic) from T1 from T2 to T1 to T2 {}

See on try.haxe.org.

Tom

unread,
Aug 29, 2014, 5:26:11 PM8/29/14
to haxe...@googlegroups.com
Thanks, it maybe works.

Then only one more problem remains: can be detect type of an array, if it is strictly typed?
Reply all
Reply to author
Forward
0 new messages