Suggestions to simplify the use of flash.Vectors?

143 views
Skip to first unread message

Joshua Granick

unread,
Mar 23, 2012, 4:45:41 PM3/23/12
to haxe...@googlegroups.com
The flash.Vector class is a bother.


You can't use a normal cast...


var vertices = cast ([1, 2, 3, 4], Vector<Int>);


...because the type parameter has to be dynamic.

You can't use a generic cast...


var vertices:Vector<Int> = cast [1, 2, 3, 4];


... because these are separate types at runtime in Flash.

You can use Vector.ofArray...


var vertices = Vector.ofArray ([1, 2, 3, 4]);


...but now you can't use it with other NME targets.


NME uses a typedef to make nme.Vector<T> the same as Array<T>


You can't extend Array<T>...


class Vector<T> extends Array<T>


...because basic types (like Array) can't be extended

You can't create a new data type...


class Vector<T> implements ArrayAccess<T>


...because ArrayAccess<T> won't actually be supported on the target
platform for the new type

You can't add a "using" mix-in...


class VectorHelper {

static function ofArray (v:Vector<T>, a:Array<T>):Vector<T> {

return a;

}

}


...because this would work on vector instances, not the static Vector class

So how do we cleanly implement an API that requires the Vector type?

It would be wonderful to have a nice solution that doesn't require
conditional code, or some kind of VectorUtils class

Dominic Graefen

unread,
Mar 23, 2012, 4:49:07 PM3/23/12
to haxe...@googlegroups.com
What's the reason that haXe Array<T> doesn't map to Vector<T> with a flash target?
The instance APIs?

Would be the ideal solution from my point of view, if possible.

-- 
Dominic Graefen
Freelance: Interactive Developer / Creative Technologist


Heinz Hölzer

unread,
Mar 23, 2012, 4:55:46 PM3/23/12
to haxe...@googlegroups.com

How about:

class VectorHelper {

public static inline function ofArray (v:Class<Vector<T>>,

a:Array<T>):Vector<T> {

return cast a;

Joshua Granick

unread,
Mar 23, 2012, 6:26:57 PM3/23/12
to haxe...@googlegroups.com
Thanks! I didn't think of Class<>

That was almost it. This works:


typedef Vector<T> = Array<T>;

class VectorHelper
{

public static inline function ofArray<T>(v:Class<Vector<Dynamic>>,
array:Array<T>):Vector<T>
{
return array;
}

}

This really helps.

Is there a way to implement this kind of functionality without requiring
"using nme.Vector"?

It would be great if this were added automatically when you imported the
nme.Vector class

Heinz Hölzer

unread,
Mar 23, 2012, 7:40:40 PM3/23/12
to haxe...@googlegroups.com
I don't think so, but i requested such a feature some time ago (http://lists.motion-twin.com/pipermail/haxe/2010-November/039542.html).
Maybe it should be added to the issues list as a feature request ;)

However, maybe you can use another nice hacky feature which acts a little bit like haskell newtypes:

@:native("Array") // it gets compiled as an Array<T>, but acts as a different type
extern class Vector<T> {
  public static inline function fromArray<T>(a:Array<T>):Vector<T> {
    return cast a;
  }
 
  inline function unbox<T>():Array<T> {
    return cast this;
  }
 
 
  public inline function push<T>(elem:T):Void {
    this.unbox().push(elem);
  }
}

usage:
var vec = Vector.fromArray([1,2,3]);
vec.push(4);

Simon Krajewski

unread,
Mar 23, 2012, 7:43:51 PM3/23/12
to haxe...@googlegroups.com
Am 23.03.2012 23:26, schrieb Joshua Granick:
> Is there a way to implement this kind of functionality without
> requiring "using nme.Vector"?
>
> It would be great if this were added automatically when you imported
> the nme.Vector class

I would actually reverse that and have "using" imply "import".

Simon

Heinz Hölzer

unread,
Mar 23, 2012, 7:54:39 PM3/23/12
to haxe...@googlegroups.com

I prefer a distinction between them, but some sugar like:

import using MyType;

would be nice.

Simon Krajewski

unread,
Mar 23, 2012, 8:26:00 PM3/23/12
to haxe...@googlegroups.com
Am 24.03.2012 00:54, schrieb Heinz H�lzer:
>
> I prefer a distinction between them, but some sugar like:
>
> import using MyType;
>
> would be nice.

That'd work for me as well, but unlike the direct implication this would
require a parser change. I'm not sure if the implication has a drawback
which would justify that. Either way is fine with me though.

Simon

Hugh

unread,
Mar 23, 2012, 9:35:12 PM3/23/12
to haXe
Joshua,
I was thinking it might be good to have a generic "bucket" nme helper
class for compatibility reasons.
You could put vector in there, but also inject routines to help with
problems of RGBA on neko without the thirty-second bit.
ie, add BitmapData.create that take (rgb:Int, a:Int).

so you would just "import nme.Helpers" or "nme.Compat"

I also can't see where you would "use" a class without "importing" it
- so might be a good shorthand.

Hugh

On Mar 24, 6:26 am, "Joshua Granick" <bulkm...@joshuagranick.com>
wrote:

Nicolas Cannasse

unread,
Mar 24, 2012, 3:01:36 AM3/24/12
to haxe...@googlegroups.com
Le 23/03/2012 21:49, Dominic Graefen a �crit :

> What's the reason that haXe Array<T> doesn't map to Vector<T> with a
> flash target?
> The instance APIs?

There is a difference between the two in the native APIs and many other
places. For instance String.split returns an Array, while other API
methods uses Vectors. So we can't compile everything to either Array or
Vector.

BTW the AVM2 support for Vectors is not very good : they don't bring
that much speed improvement over Arrays and this they are not real generics.

Best,
Nicolas

Juraj Kirchheim

unread,
Mar 24, 2012, 8:06:58 AM3/24/12
to haxe...@googlegroups.com
Yes, that's a workable solution.
Just a slight enhancement:

@:native("Array") extern class Vector<T> implements
ArrayAccess<T> { ... }

The only drawback here is, that at runtime you have no way of figuring
out whether something was created as an Array or a Vector, but I think
that's ok.

Regards,
Juraj

Reply all
Reply to author
Forward
0 new messages