Copying a class instance, not creating a new reference

classic Classic list List threaded Threaded
18 messages Options
Reply | Threaded
Open this post in threaded view
|

Copying a class instance, not creating a new reference

Nathan Huesken
Hi,

I am sure, this is documented somewhere. But I am just not able to find it. Maybe I do not know the correct keywords ...

If I do the following:

class Vector2D
{
  var x:Float;
  var y:Float;
}

...

var A:Vector2D;
...
var B:Vector2D;
b=a;
b.x=100;

The last line changes the x propertie of A, because B is a reference to A not a copy.
How can I make a real copy of a class instance?

Thanks!
Nathan

--
haXe - an open source web programming language
http://haxe.org
Reply | Threaded
Open this post in threaded view
|

Re: Copying a class instance, not creating a new reference

Tony Polinelli


you need to either use the .clone() method on the vector class (providing there is one). It will basically be doing code similar to this.

public function clone():vector2D
{
     return new Vector2D(x,y);
}

as far as i know primitive types are always assigned as a copy in flash whereas complex types are assigned by reference



On Sun, Feb 8, 2009 at 11:05 AM, Nathan Huesken <[hidden email]> wrote:
Hi,

I am sure, this is documented somewhere. But I am just not able to find it. Maybe I do not know the correct keywords ...

If I do the following:

class Vector2D
{
 var x:Float;
 var y:Float;
}

...

var A:Vector2D;
...
var B:Vector2D;
b=a;
b.x=100;

The last line changes the x propertie of A, because B is a reference to A not a copy.
How can I make a real copy of a class instance?

Thanks!
Nathan

--
haXe - an open source web programming language
http://haxe.org



--
Tony Polinelli
http://www.touchmypixel.com

--
haXe - an open source web programming language
http://haxe.org
Reply | Threaded
Open this post in threaded view
|

Re: Copying a class instance, not creating a new reference

Ian Martins
In reply to this post by Nathan Huesken
Nathan Huesken wrote:

> Hi,
>
> I am sure, this is documented somewhere. But I am just not able to find it. Maybe I do not know the correct keywords ...
>
> If I do the following:
>
> class Vector2D
> {
>   var x:Float;
>   var y:Float;
> }
>
> ...
>
> var A:Vector2D;
> ...
> var B:Vector2D;
> b=a;
> b.x=100;
>
> The last line changes the x propertie of A, because B is a reference to A not a copy.
> How can I make a real copy of a class instance?
>
> Thanks!
> Nathan
>
>  
for simple types like that, you can use Reflect.copy().



--
haXe - an open source web programming language
http://haxe.org
Reply | Threaded
Open this post in threaded view
|

Re: Copying a class instance, not creating a new reference

Tony Polinelli
That creates an anonymous object and then just copies the field values:

var o2 : Dynamic = {};
            for( f in Reflect.fields(o) )
                Reflect.setField(o2,f,Reflect.field(o,f));
            return o2;

that that isnt a proper copy of an instance - do a trace of waht it returns and it is prints [object Object]  not a vector



On Sun, Feb 8, 2009 at 11:31 AM, Ian Martins <[hidden email]> wrote:
Nathan Huesken wrote:
Hi,

I am sure, this is documented somewhere. But I am just not able to find it. Maybe I do not know the correct keywords ...

If I do the following:

class Vector2D
{
 var x:Float;
 var y:Float;
}

...

var A:Vector2D;
...
var B:Vector2D;
b=a;
b.x=100;

The last line changes the x propertie of A, because B is a reference to A not a copy.
How can I make a real copy of a class instance?

Thanks!
Nathan

 
for simple types like that, you can use Reflect.copy().




--
haXe - an open source web programming language
http://haxe.org



--
Tony Polinelli
http://www.touchmypixel.com

--
haXe - an open source web programming language
http://haxe.org
Reply | Threaded
Open this post in threaded view
|

Re: Copying a class instance, not creating a new reference

Ian Martins
it is templated.  it returns the type passed in.  it won't work for
complex types, but for a simple type like the example should be ok, right?


Tony Polinelli wrote:

> That creates an anonymous object and then just copies the field values:
>
> var o2 : Dynamic = {};
>             for( f in Reflect.fields(o) )
>                 Reflect.setField(o2,f,Reflect.field(o,f));
>             return o2;
>
> that that isnt a proper copy of an instance - do a trace of waht it
> returns and it is prints [object Object]  not a vector
>
>
>
> On Sun, Feb 8, 2009 at 11:31 AM, Ian Martins <[hidden email]
> <mailto:[hidden email]>> wrote:
>
>     Nathan Huesken wrote:
>
>         Hi,
>
>         I am sure, this is documented somewhere. But I am just not
>         able to find it. Maybe I do not know the correct keywords ...
>
>         If I do the following:
>
>         class Vector2D
>         {
>          var x:Float;
>          var y:Float;
>         }
>
>         ...
>
>         var A:Vector2D;
>         ...
>         var B:Vector2D;
>         b=a;
>         b.x=100;
>
>         The last line changes the x propertie of A, because B is a
>         reference to A not a copy.
>         How can I make a real copy of a class instance?
>
>         Thanks!
>         Nathan
>
>          
>
>     for simple types like that, you can use Reflect.copy().
>
>
>
>
>     --
>     haXe - an open source web programming language
>     http://haxe.org
>
>
>
>
> --
> Tony Polinelli
> http://www.touchmypixel.com


--
haXe - an open source web programming language
http://haxe.org
Reply | Threaded
Open this post in threaded view
|

Re: Copying a class instance, not creating a new reference

Tony Polinelli

I was assuming that the vector2D class was a general vector class which would have a whole lot of functionality.

just as a test (cause i havent really used the copy function) i've tried

var v:Vector2D = new Vector2D();
v.x = 34;
v.y = 35;
trace(v);
       
var b:Vector2D = Reflect.copy(v);    
trace(b);

class Vector2D {
   
    public var x:Float;
    public var y:Float;
   
    public function new()
    {
       
    }
}

this fails to convert Dynamic to Vector2D - despite the class taking T and returning T - i must be doing something wrong.


On Sun, Feb 8, 2009 at 2:06 PM, Ian Martins <[hidden email]> wrote:
it is templated.  it returns the type passed in.  it won't work for complex types, but for a simple type like the example should be ok, right?


Tony Polinelli wrote:
That creates an anonymous object and then just copies the field values:

var o2 : Dynamic = {};
           for( f in Reflect.fields(o) )
               Reflect.setField(o2,f,Reflect.field(o,f));
           return o2;

that that isnt a proper copy of an instance - do a trace of waht it returns and it is prints [object Object]  not a vector



On Sun, Feb 8, 2009 at 11:31 AM, Ian Martins <[hidden email] <mailto:[hidden email]>> wrote:

   Nathan Huesken wrote:

       Hi,

       I am sure, this is documented somewhere. But I am just not
       able to find it. Maybe I do not know the correct keywords ...

       If I do the following:

       class Vector2D
       {
        var x:Float;
        var y:Float;
       }

       ...

       var A:Vector2D;
       ...
       var B:Vector2D;
       b=a;
       b.x=100;

       The last line changes the x propertie of A, because B is a
       reference to A not a copy.
       How can I make a real copy of a class instance?

       Thanks!
       Nathan

       
   for simple types like that, you can use Reflect.copy().




   --    haXe - an open source web programming language
   http://haxe.org




--
Tony Polinelli
http://www.touchmypixel.com


--
haXe - an open source web programming language
http://haxe.org



--
Tony Polinelli
http://www.touchmypixel.com

--
haXe - an open source web programming language
http://haxe.org
Reply | Threaded
Open this post in threaded view
|

Re: Copying a class instance, not creating a new reference

Ian Martins
I did this:
--
class VecCpy
{          
  public static function main()
  {
    var a = new Vec();
    a.x = 1;
    a.y = 2;

    var b = Reflect.copy(a);

    trace(a);
    trace(b);

    trace(Type.typeof(a));
    trace(Type.typeof(b));

    a.x = 2;

    trace(a);
    trace(b);
  }
}

class Vec
{
  public var x:Float;
  public var y:Float;

  public function new()
  {}
}

--
and got this:
--
VecCpy.hx:11: { x => 1, y => 2 }
VecCpy.hx:12: { x => 1, y => 2 }
VecCpy.hx:14: TClass({ __name__ => [Vec], __construct__ => #function:0,
prototype => { __class__ => ..., x => null, y => null, __serialize =>
#function:0 },$
VecCpy.hx:15: TClass({ __name__ => [Vec], __construct__ => #function:0,
prototype => { __class__ => ..., x => null, y => null, __serialize =>
#function:0 },$
VecCpy.hx:19: { x => 2, y => 2 }
VecCpy.hx:20: { x => 1, y => 2 }




Tony Polinelli wrote:

>
> I was assuming that the vector2D class was a general vector class
> which would have a whole lot of functionality.
>
> just as a test (cause i havent really used the copy function) i've tried
>
> var v:Vector2D = new Vector2D();
> v.x = 34;
> v.y = 35;
> trace(v);
>        
> var b:Vector2D = Reflect.copy(v);    
> trace(b);
>
> class Vector2D {
>    
>     public var x:Float;
>     public var y:Float;
>    
>     public function new()
>     {
>        
>     }
> }
>
> this fails to convert Dynamic to Vector2D - despite the class taking T
> and returning T - i must be doing something wrong.
>
>
> On Sun, Feb 8, 2009 at 2:06 PM, Ian Martins <[hidden email]
> <mailto:[hidden email]>> wrote:
>
>     it is templated.  it returns the type passed in.  it won't work
>     for complex types, but for a simple type like the example should
>     be ok, right?
>
>
>     Tony Polinelli wrote:
>
>         That creates an anonymous object and then just copies the
>         field values:
>
>         var o2 : Dynamic = {};
>                    for( f in Reflect.fields(o) )
>                        Reflect.setField(o2,f,Reflect.field(o,f));
>                    return o2;
>
>         that that isnt a proper copy of an instance - do a trace of
>         waht it returns and it is prints [object Object]  not a vector
>
>
>
>         On Sun, Feb 8, 2009 at 11:31 AM, Ian Martins <[hidden email]
>         <mailto:[hidden email]> <mailto:[hidden email]
>         <mailto:[hidden email]>>> wrote:
>
>            Nathan Huesken wrote:
>
>                Hi,
>
>                I am sure, this is documented somewhere. But I am just not
>                able to find it. Maybe I do not know the correct
>         keywords ...
>
>                If I do the following:
>
>                class Vector2D
>                {
>                 var x:Float;
>                 var y:Float;
>                }
>
>                ...
>
>                var A:Vector2D;
>                ...
>                var B:Vector2D;
>                b=a;
>                b.x=100;
>
>                The last line changes the x propertie of A, because B is a
>                reference to A not a copy.
>                How can I make a real copy of a class instance?
>
>                Thanks!
>                Nathan
>
>                
>            for simple types like that, you can use Reflect.copy().
>
>
>
>
>            --    haXe - an open source web programming language
>            http://haxe.org
>
>
>
>
>         --
>         Tony Polinelli
>         http://www.touchmypixel.com
>
>
>
>     --
>     haXe - an open source web programming language
>     http://haxe.org
>
>
>
>
> --
> Tony Polinelli
> http://www.touchmypixel.com


--
haXe - an open source web programming language
http://haxe.org
Reply | Threaded
Open this post in threaded view
|

Re: Copying a class instance, not creating a new reference

Tony Polinelli
I've run your code with flash 9 as a target i get:
TypeError: Error #1034: Type Coercion failed: cannot convert Object@8fe0fd9 to scarygirl.site.Vec.
    at scarygirl.site::VecCpy$/main()
    at scarygirl.site::Main$/main()
    at MethodInfo-128()
    at Boot_b99dcb()


with php as a target i get:

VecCpy.hx:85: scarygirl.cms.Vec
VecCpy.hx:86: { x : 1, y : 2 }
VecCpy.hx:88: TClass(scarygirl.cms.Vec)
VecCpy.hx:89: TObject
VecCpy.hx:93: scarygirl.cms.Vec
VecCpy.hx:94: { x : 1, y : 2 }

this is similar to your experience just with - but flash obviously bugs. i'm running haxe 2.02

It seems not to work too well crossplatform - were you testing it as neko?





On Sun, Feb 8, 2009 at 3:31 PM, Ian Martins <[hidden email]> wrote:
I did this:
--
class VecCpy
{           public static function main()
 {
  var a = new Vec();
  a.x = 1;
  a.y = 2;

  var b = Reflect.copy(a);

  trace(a);
  trace(b);

  trace(Type.typeof(a));
  trace(Type.typeof(b));

  a.x = 2;

  trace(a);
  trace(b);
 }
}

class Vec

{
 public var x:Float;
 public var y:Float;

 public function new()
 {}
}

--
and got this:
--
VecCpy.hx:11: { x => 1, y => 2 }
VecCpy.hx:12: { x => 1, y => 2 }
VecCpy.hx:14: TClass({ __name__ => [Vec], __construct__ => #function:0, prototype => { __class__ => ..., x => null, y => null, __serialize => #function:0 },$
VecCpy.hx:15: TClass({ __name__ => [Vec], __construct__ => #function:0, prototype => { __class__ => ..., x => null, y => null, __serialize => #function:0 },$
VecCpy.hx:19: { x => 2, y => 2 }
VecCpy.hx:20: { x => 1, y => 2 }




Tony Polinelli wrote:

I was assuming that the vector2D class was a general vector class which would have a whole lot of functionality.

just as a test (cause i havent really used the copy function) i've tried

var v:Vector2D = new Vector2D();
v.x = 34;
v.y = 35;
trace(v);
      var b:Vector2D = Reflect.copy(v);    trace(b);

class Vector2D {
     public var x:Float;
   public var y:Float;
     public function new()
   {
         }
}

this fails to convert Dynamic to Vector2D - despite the class taking T and returning T - i must be doing something wrong.


On Sun, Feb 8, 2009 at 2:06 PM, Ian Martins <[hidden email] <mailto:[hidden email]>> wrote:

   it is templated.  it returns the type passed in.  it won't work
   for complex types, but for a simple type like the example should
   be ok, right?


   Tony Polinelli wrote:

       That creates an anonymous object and then just copies the
       field values:

       var o2 : Dynamic = {};
                  for( f in Reflect.fields(o) )
                      Reflect.setField(o2,f,Reflect.field(o,f));
                  return o2;

       that that isnt a proper copy of an instance - do a trace of
       waht it returns and it is prints [object Object]  not a vector



       On Sun, Feb 8, 2009 at 11:31 AM, Ian Martins <[hidden email]
       <mailto:[hidden email]> <mailto:[hidden email]

       <mailto:[hidden email]>>> wrote:

          Nathan Huesken wrote:

              Hi,

              I am sure, this is documented somewhere. But I am just not
              able to find it. Maybe I do not know the correct
       keywords ...

              If I do the following:

              class Vector2D
              {
               var x:Float;
               var y:Float;
              }

              ...

              var A:Vector2D;
              ...
              var B:Vector2D;
              b=a;
              b.x=100;

              The last line changes the x propertie of A, because B is a
              reference to A not a copy.
              How can I make a real copy of a class instance?

              Thanks!
              Nathan

                        for simple types like that, you can use Reflect.copy().




          --    haXe - an open source web programming language
          http://haxe.org




       --        Tony Polinelli
       http://www.touchmypixel.com



   --    haXe - an open source web programming language
   http://haxe.org




--
Tony Polinelli
http://www.touchmypixel.com


--
haXe - an open source web programming language
http://haxe.org



--
Tony Polinelli
http://www.touchmypixel.com

--
haXe - an open source web programming language
http://haxe.org
Reply | Threaded
Open this post in threaded view
|

Re: Copying a class instance, not creating a new reference

Ian Martins
yes, I was using neko on 2.02.  I didn't expect different results on
other targets.

Tony Polinelli wrote:

> I've run your code with flash 9 as a target i get:
> TypeError: Error #1034: Type Coercion failed: cannot convert
> Object@8fe0fd9 to scarygirl.site.Vec.
>     at scarygirl.site::VecCpy$/main()
>     at scarygirl.site::Main$/main()
>     at MethodInfo-128()
>     at Boot_b99dcb()
>
>
> with php as a target i get:
>
> VecCpy.hx:85: scarygirl.cms.Vec
> VecCpy.hx:86: { x : 1, y : 2 }
> VecCpy.hx:88: TClass(scarygirl.cms.Vec)
> VecCpy.hx:89: TObject
> VecCpy.hx:93: scarygirl.cms.Vec
> VecCpy.hx:94: { x : 1, y : 2 }
>
> this is similar to your experience just with - but flash obviously
> bugs. i'm running haxe 2.02
>
> It seems not to work too well crossplatform - were you testing it as neko?
>
>
>
>
>
> On Sun, Feb 8, 2009 at 3:31 PM, Ian Martins <[hidden email]
> <mailto:[hidden email]>> wrote:
>
>     I did this:
>     --
>     class VecCpy
>     {           public static function main()
>      {
>       var a = new Vec();
>       a.x = 1;
>       a.y = 2;
>
>       var b = Reflect.copy(a);
>
>       trace(a);
>       trace(b);
>
>       trace(Type.typeof(a));
>       trace(Type.typeof(b));
>
>       a.x = 2;
>
>       trace(a);
>       trace(b);
>      }
>     }
>
>     class Vec
>
>     {
>      public var x:Float;
>      public var y:Float;
>
>      public function new()
>      {}
>     }
>
>     --
>     and got this:
>     --
>     VecCpy.hx:11: { x => 1, y => 2 }
>     VecCpy.hx:12: { x => 1, y => 2 }
>     VecCpy.hx:14: TClass({ __name__ => [Vec], __construct__ =>
>     #function:0, prototype => { __class__ => ..., x => null, y =>
>     null, __serialize => #function:0 },$
>     VecCpy.hx:15: TClass({ __name__ => [Vec], __construct__ =>
>     #function:0, prototype => { __class__ => ..., x => null, y =>
>     null, __serialize => #function:0 },$
>     VecCpy.hx:19: { x => 2, y => 2 }
>     VecCpy.hx:20: { x => 1, y => 2 }
>
>
>
>
>     Tony Polinelli wrote:
>
>
>         I was assuming that the vector2D class was a general vector
>         class which would have a whole lot of functionality.
>
>         just as a test (cause i havent really used the copy function)
>         i've tried
>
>         var v:Vector2D = new Vector2D();
>         v.x = 34;
>         v.y = 35;
>         trace(v);
>               var b:Vector2D = Reflect.copy(v);    trace(b);
>
>         class Vector2D {
>              public var x:Float;
>            public var y:Float;
>              public function new()
>            {
>                  }
>         }
>
>         this fails to convert Dynamic to Vector2D - despite the class
>         taking T and returning T - i must be doing something wrong.
>
>
>         On Sun, Feb 8, 2009 at 2:06 PM, Ian Martins <[hidden email]
>         <mailto:[hidden email]> <mailto:[hidden email]
>         <mailto:[hidden email]>>> wrote:
>
>            it is templated.  it returns the type passed in.  it won't work
>            for complex types, but for a simple type like the example
>         should
>            be ok, right?
>
>
>            Tony Polinelli wrote:
>
>                That creates an anonymous object and then just copies the
>                field values:
>
>                var o2 : Dynamic = {};
>                           for( f in Reflect.fields(o) )
>                               Reflect.setField(o2,f,Reflect.field(o,f));
>                           return o2;
>
>                that that isnt a proper copy of an instance - do a trace of
>                waht it returns and it is prints [object Object]  not a
>         vector
>
>
>
>                On Sun, Feb 8, 2009 at 11:31 AM, Ian Martins
>         <[hidden email] <mailto:[hidden email]>
>                <mailto:[hidden email] <mailto:[hidden email]>>
>         <mailto:[hidden email] <mailto:[hidden email]>
>
>                <mailto:[hidden email] <mailto:[hidden email]>>>> wrote:
>
>                   Nathan Huesken wrote:
>
>                       Hi,
>
>                       I am sure, this is documented somewhere. But I
>         am just not
>                       able to find it. Maybe I do not know the correct
>                keywords ...
>
>                       If I do the following:
>
>                       class Vector2D
>                       {
>                        var x:Float;
>                        var y:Float;
>                       }
>
>                       ...
>
>                       var A:Vector2D;
>                       ...
>                       var B:Vector2D;
>                       b=a;
>                       b.x=100;
>
>                       The last line changes the x propertie of A,
>         because B is a
>                       reference to A not a copy.
>                       How can I make a real copy of a class instance?
>
>                       Thanks!
>                       Nathan
>
>                                 for simple types like that, you can
>         use Reflect.copy().
>
>
>
>
>                   --    haXe - an open source web programming language
>                   http://haxe.org
>
>
>
>
>                --        Tony Polinelli
>                http://www.touchmypixel.com
>
>
>
>            --    haXe - an open source web programming language
>            http://haxe.org
>
>
>
>
>         --
>         Tony Polinelli
>         http://www.touchmypixel.com
>
>
>
>     --
>     haXe - an open source web programming language
>     http://haxe.org
>
>
>
>
> --
> Tony Polinelli
> http://www.touchmypixel.com


--
haXe - an open source web programming language
http://haxe.org
Reply | Threaded
Open this post in threaded view
|

Re: Copying a class instance, not creating a new reference

Nicolas Cannasse
Ian Martins a écrit :
> yes, I was using neko on 2.02.  I didn't expect different results on
> other targets.

Reflect.copy works well with instances on Neko only. It is only
guaranteed to works with anonymous objects on all platforms.

Best,
Nicolas

--
haXe - an open source web programming language
http://haxe.org
Reply | Threaded
Open this post in threaded view
|

Re: Copying a class instance, not creating a new reference

Ian Martins
Nicolas Cannasse wrote:
> Ian Martins a écrit :
>> yes, I was using neko on 2.02.  I didn't expect different results on
>> other targets.
>
> Reflect.copy works well with instances on Neko only. It is only
> guaranteed to works with anonymous objects on all platforms.
>
thanks for clarification. I updated the doc.

--
haXe - an open source web programming language
http://haxe.org
Reply | Threaded
Open this post in threaded view
|

how to get Class Property in haxe?

lulersoft自动发信机器人
In reply to this post by Tony Polinelli

here the class:
class Sam{
public var go_1_str:Int;
public var go_2_str:Int;
public var go_3_str:Int;
....
}
 
how can conversion below AS3 Code to haxe?
 
var vaule:int =4;
while(vaule-- >0){
trace Sam["go_"+vaule+"str"];
}
 
 
谢谢你们。
 
 

--
haXe - an open source web programming language
http://haxe.org
Reply | Threaded
Open this post in threaded view
|

Re: how to get Class Property in haxe?

Owen Durni-2
On Tue, Feb 10, 2009 at 12:18 AM, lulersoft自动发信机器人 <[hidden email]> wrote:

> here the class:
> class Sam{
> public var go_1_str:Int;
> public var go_2_str:Int;
> public var go_3_str:Int;
> ....
> }
>
> how can conversion below AS3 Code to haxe?
>
> var vaule:int =4;
> while(vaule-- >0){
> trace Sam["go_"+vaule+"str"];
> }

It really looks like an array of go_str would be cleaner than listing
them all out.

--
haXe - an open source web programming language
http://haxe.org
Reply | Threaded
Open this post in threaded view
|

Re: Re: how to get Class Property in haxe?

lulersoft自动发信机器人
class Sam{
public var go_1_str:Int;
public var go_2_str:Int;
public var go_3_str:Int;
public var n:Int;
....
public functiom /*new()*/Sam(){
go_1_str=1;
go_1_str=22;
go_1_str=3;
n=0;
}
.....
}
 
 
in other class (as3):
var vaule:int =4;
while(vaule-- >0){
trace Sam["go_"+vaule+"str"];
}
 
 
and haxe code??
 

lulersoft自动发信机器人
2009-02-10

发件人: Owen Durni
发送时间: 2009-02-10 14:19:29
收件人: The haXe compiler list
抄送:
主题: Re: [haXe] how to get Class Property in haxe?
 
On Tue, Feb 10, 2009 at 12:18 AM, lulersoft自动发信机器人 <[hidden email]> wrote:
> here the class:
> class Sam{
> public var go_1_str:Int;
> public var go_2_str:Int;
> public var go_3_str:Int;
> ....
> }
>
> how can conversion below AS3 Code to haxe?
>
> var vaule:int =4;
> while(vaule-- >0){
> trace Sam["go_"+vaule+"str"];
> }
It really looks like an array of go_str would be cleaner than listing
them all out.
-- 
haXe - an open source web programming language
http://haxe.org
__________ Information from ESET NOD32 Antivirus, version of virus signature database 3840 (20090210) __________
The message was checked by ESET NOD32 Antivirus.
http://www.eset.com

--
haXe - an open source web programming language
http://haxe.org
Reply | Threaded
Open this post in threaded view
|

Re: Re: how to get Class Property in haxe?

lulersoft自动发信机器人
In reply to this post by Owen Durni-2
class Sam{
public var go_1_str:Int;
public var go_2_str:Int;
public var go_3_str:Int;
public var n:Int;
....
public functiom /*new()*/Sam(){
go_1_str=1;
go_1_str=22;
go_1_str=3;
n=0;
}
.....
}
 
 
in other class (as3):
var vaule:int =4;
while(vaule-- >0){
trace Sam["go_"+vaule+"str"];
}
 
 
and haxe code??
 

lulersoft自动发信机器人
2009-02-10

发件人: Owen Durni
发送时间: 2009-02-10 14:19:29
收件人: The haXe compiler list
抄送:
主题: Re: [haXe] how to get Class Property in haxe?
 
On Tue, Feb 10, 2009 at 12:18 AM, lulersoft自动发信机器人 <[hidden email]> wrote:
> here the class:
> class Sam{
> public var go_1_str:Int;
> public var go_2_str:Int;
> public var go_3_str:Int;
> ....
> }
>
> how can conversion below AS3 Code to haxe?
>
> var vaule:int =4;
> while(vaule-- >0){
> trace Sam["go_"+vaule+"str"];
> }
It really looks like an array of go_str would be cleaner than listing
them all out.
-- 
haXe - an open source web programming language
http://haxe.org
__________ Information from ESET NOD32 Antivirus, version of virus signature database 3840 (20090210) __________
The message was checked by ESET NOD32 Antivirus.
http://www.eset.com

--
haXe - an open source web programming language
http://haxe.org
Reply | Threaded
Open this post in threaded view
|

Re: Re: how to get Class Property in haxe?

Gamehaxe
Hi,
Try:
  var s = new Sam();
  trace( Reflect.field(s,"go_"+vaule+"str"));

Hugh


> class Sam{
> public var go_1_str:Int;
> public var go_2_str:Int;
> public var go_3_str:Int;
> public var n:Int;
> ....
> public functiom /*new()*/Sam(){
> go_1_str=1;
> go_1_str=22;
> go_1_str=3;
> n=0;
> }
> .....
> }
>
>
> in other class (as3):
> var vaule:int =4;
> while(vaule-- >0){
> trace Sam["go_"+vaule+"str"];
> }
>
>
> and haxe code??
>
>
>
>
> lulersoft自动发信机器人
> 2009-02-10
>
>
>
> 发件人: Owen Durni
> 发送时间: 2009-02-10 14:19:29
> 收件人: The haXe compiler list
> 抄送:
> 主题: Re: [haXe] how to get Class Property in haxe?
>
> On Tue, Feb 10, 2009 at 12:18 AM, lulersoft自动发信机器人  
> <[hidden email]> wrote:
>> here the class:
>> class Sam{
>> public var go_1_str:Int;
>> public var go_2_str:Int;
>> public var go_3_str:Int;
>> ....
>> }
>>
>> how can conversion below AS3 Code to haxe?
>>
>> var vaule:int =4;
>> while(vaule-- >0){
>> trace Sam["go_"+vaule+"str"];
>> }
> It really looks like an array of go_str would be cleaner than listing
> them all out.



--
haXe - an open source web programming language
http://haxe.org
Reply | Threaded
Open this post in threaded view
|

Re: Re: Re: how to get Class Property in haxe?

lulersoft自动发信机器人

think you,it's work!!!
 

lulersoft自动发信机器人
2009-02-11

发件人: Hugh Sanderson
发送时间: 2009-02-10 17:50:25
收件人: The haXe compiler list
抄送:
主题: Re: Re: [haXe] how to get Class Property in haxe?
 
Hi,
Try:
  var s = new Sam();
  trace( Reflect.field(s,"go_"+vaule+"str"));
Hugh
> class Sam{
> public var go_1_str:Int;
> public var go_2_str:Int;
> public var go_3_str:Int;
> public var n:Int;
> ....
> public functiom /*new()*/Sam(){
> go_1_str=1;
> go_1_str=22;
> go_1_str=3;
> n=0;
> }
> .....
> }
>
>
> in other class (as3):
> var vaule:int =4;
> while(vaule-- >0){
> trace Sam["go_"+vaule+"str"];
> }
>
>
> and haxe code??
>
>
>
>
> lulersoft自动发信机器人
> 2009-02-10
>
>
>
> 发件人: Owen Durni
> 发送时间: 2009-02-10 14:19:29
> 收件人: The haXe compiler list
> 抄送:
> 主题: Re: [haXe] how to get Class Property in haxe?
>
> On Tue, Feb 10, 2009 at 12:18 AM, lulersoft自动发信机器人  
> <[hidden email]> wrote:
>> here the class:
>> class Sam{
>> public var go_1_str:Int;
>> public var go_2_str:Int;
>> public var go_3_str:Int;
>> ....
>> }
>>
>> how can conversion below AS3 Code to haxe?
>>
>> var vaule:int =4;
>> while(vaule-- >0){
>> trace Sam["go_"+vaule+"str"];
>> }
> It really looks like an array of go_str would be cleaner than listing
> them all out.
-- 
haXe - an open source web programming language
http://haxe.org
__________ Information from ESET NOD32 Antivirus, version of virus signature database 3840 (20090210) __________
The message was checked by ESET NOD32 Antivirus.
http://www.eset.com

--
haXe - an open source web programming language
http://haxe.org
Reply | Threaded
Open this post in threaded view
|

Re: how to get Class Property in haxe?

edA-qa mort-ora-y
In reply to this post by lulersoft自动发信机器人
lulersoft自动发信机器人 wrote:
> how can conversion below AS3 Code to haxe?
> var vaule:int =4;
> while(vaule-- >0){
> trace Sam["go_"+vaule+"str"];
> }

In my DHLIB (available in haxelib) is a DynamicUtil which contains a
defGet function (get field, or use default if not defined).

http://www.disemia.com/dhlib/doc/api/content/DynamicUtil.html

Otherwise you can simply use Reflect.field( object, field ) where field
is the name of the member you wish to access.



--
edA-qa mort-ora-y
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --

The dis-Emi-A haXe Library
        http://wiki.disemia.com/HaXe
       
A full set of tools, classes, and support facilities aimed at simplifying
and expediating game creation in Flash 9.

-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
Sign: Please digitally sign your emails.
Encrypt: I'm also happy to receive encrypted mail.


--
haXe - an open source web programming language
http://haxe.org

signature.asc (260 bytes) Download Attachment