I wrote a function that makes a deep copy of any type. I started from
franco's function for arrays. posting it in case it helps anyone. and in case someone sees something wrong with it. it's 5x faster than my first attempt... haxe.Unserializer.run(haxe.Serializer.run(v)). /** deep copy of anything **/ public static function deepCopy<T>( v:T ) : T { if (!Reflect.isObject(v)) // simple type { return v; } else if( Std.is( v, Array ) ) // array { var r = Type.createInstance(Type.getClass(v), []); untyped { for( ii in 0...v.length ) r.push(deepCopy(v[ii])); } return r; } else if( Type.getClass(v) == null ) // anonymous object { var obj : Dynamic = {}; for( ff in Reflect.fields(v) ) Reflect.setField(obj, ff, deepCopy(Reflect.field(v, ff))); return obj; } else // class { var obj = Type.createEmptyInstance(Type.getClass(v)); for( ff in Reflect.fields(v) ) Reflect.setField(obj, ff, deepCopy(Reflect.field(v, ff))); return obj; } return null; } -- haXe - an open source web programming language http://haxe.org |
Could that be used for a snoop style tool?
http://www.blois.us/Snoop/ On 6 Jan 2009, at 23:03, Ian Martins wrote: > I wrote a function that makes a deep copy of any type. I started > from franco's function for arrays. posting it in case it helps > anyone. and in case someone sees something wrong with it. it's 5x > faster than my first attempt... haxe.Unserializer.run > (haxe.Serializer.run(v)). > > > /** > deep copy of anything > **/ > public static function deepCopy<T>( v:T ) : T > { > if (!Reflect.isObject(v)) // simple type > { > return v; > } > else if( Std.is( v, Array ) ) // array > { > var r = Type.createInstance(Type.getClass(v), []); > untyped > { > for( ii in 0...v.length ) > r.push(deepCopy(v[ii])); > } > return r; > } > else if( Type.getClass(v) == null ) // anonymous object > { > var obj : Dynamic = {}; > for( ff in Reflect.fields(v) ) > Reflect.setField(obj, ff, deepCopy(Reflect.field(v, ff))); > return obj; > } > else // class > { > var obj = Type.createEmptyInstance(Type.getClass(v)); > for( ff in Reflect.fields(v) ) > Reflect.setField(obj, ff, deepCopy(Reflect.field(v, ff))); > return obj; > } > return null; > } > > > > > -- > haXe - an open source web programming language > http://haxe.org -- haXe - an open source web programming language http://haxe.org |
I don't think it would help much in building a tool like that. it's
just a basic low level routine. Justin Lawerance Mills wrote: > Could that be used for a snoop style tool? > http://www.blois.us/Snoop/ > > On 6 Jan 2009, at 23:03, Ian Martins wrote: > >> I wrote a function that makes a deep copy of any type. I started >> from franco's function for arrays. posting it in case it helps >> anyone. and in case someone sees something wrong with it. it's 5x >> faster than my first attempt... >> haxe.Unserializer.run(haxe.Serializer.run(v)). >> -- haXe - an open source web programming language http://haxe.org |
In reply to this post by Ian Martins
Ian Martins a écrit :
> I wrote a function that makes a deep copy of any type. I started from > franco's function for arrays. posting it in case it helps anyone. and > in case someone sees something wrong with it. it's 5x faster than my > first attempt... haxe.Unserializer.run(haxe.Serializer.run(v)). Nice. Just a suggestion : maybe using a single Type.typeof would be faster than several Std.is, and will also let you handle Enums correctly. Best, Nicolas -- haXe - an open source web programming language http://haxe.org |
This post has NOT been accepted by the mailing list yet.
I made a library for this called cloner. If it doesn't clone something you need just write an issue or make a pull request.
https://github.com/thomasuster/cloner |
Free forum by Nabble | Edit this page |