You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@royale.apache.org by gbarbosa <gb...@gmail.com> on 2021/04/20 23:24:18 UTC

Casting workarounds

Hi Royale,

I was recently trying to cast an Object

    obj as CustomVO

but it would always turn to null. The as operator kept thinking CustomVO was
type Function() why would this be?

The workaround ending up being to set the prototype.

    Object.setPrototypeOf(obj, CustomVO.prototype);

and this successfully casted correctly. Is there a better way to do this?
Most likely something something to do with the structure of CustomVO? the
classes it extends?

Thanks for your time.

-Gabriel Barbosa



--
Sent from: http://apache-royale-development.20373.n8.nabble.com/

RE: Casting workarounds

Posted by Maria Jose Esteve <mj...@iest.com>.
Perfect 😉

-----Mensaje original-----
De: GAbe Barbosa <gb...@gmail.com> 
Enviado el: miércoles, 28 de abril de 2021 18:28
Para: dev@royale.apache.org
Asunto: Re: Casting workarounds

Thanks Hiedra ,

After reading your post I diffed the upstream and downstream of that VO.
The remote class alias had slightly changed! This resolved the issue.

-Gabriel Barbosa

Re: Casting workarounds

Posted by GAbe Barbosa <gb...@gmail.com>.
Thanks Hiedra ,

After reading your post I diffed the upstream and downstream of that VO.
The remote class alias had slightly changed! This resolved the issue.

-Gabriel Barbosa

RE: Casting workarounds

Posted by Maria Jose Esteve <mj...@iest.com>.
I forget one last case that I discovered recently... if a class returned by the backend is not used explicitly, in a variable, no binding is performed and if you interrogate the variable it returns null.

Let me give you an example...
I have an ICTypeArqBaseMethod interface that implements some return classes from my backend: CTypeXXXBaseMethod. The Controller that receives the data from the back manages them through the interface, ICTypeArqBaseMethod, instead of working with typed variables, CTypeXXXBaseMethod.

To solve this, you have to explicitly register the class with org.apache.royale.reflection.registerClassAlias("com.example.CTypeXXXBaseMethod", "CTypeXXXBaseMethod") in the initialize event of Application OR create a variable, somewhere, of type " CTypeXXXXBaseMethod".

Hiedra.

-----Mensaje original-----
De: Maria Jose Esteve <mj...@iest.com> 
Enviado el: martes, 27 de abril de 2021 23:16
Para: dev@royale.apache.org
Asunto: RE: Casting workarounds

Hi, I don't know if I'm understanding correctly?
I have encountered this situation a lot of times in my project and 99.8% of the times it was because the structures were "not exactly the same", usually because of upper/lower case or because I forgot to put "[RemoteClass(alias="com.example.MyClass")]". Then I have 0.2% of casps that I can't get them to map, in these cases I simply do what Harbs said: create an object and manually map the properties.

Could you post the structure of your backend and the structure you have defined in as, maybe we can see something.

Hiedra.

-----Mensaje original-----
De: GAbe Barbosa <gb...@gmail.com> Enviado el: martes, 27 de abril de 2021 22:02
Para: dev@royale.apache.org
Asunto: Re: Casting workarounds

For clarity this is a cast that worked in Flex/Flash from the response of an AMF call. Upon investigation it looks like AMF is stripping away property class data on it's return from the server.

This is the structure. We return ResultVO from java with a property result of ChildVO.

In our Royale App we successfully cast var r:ResultVO = response as ResultVO;

but r.result is being seen as a Function() instead of ChildVO. So no matter if I cast with as operator or explicitly both fail.

RE: Casting workarounds

Posted by Maria Jose Esteve <mj...@iest.com>.
Hi, I don't know if I'm understanding correctly?
I have encountered this situation a lot of times in my project and 99.8% of the times it was because the structures were "not exactly the same", usually because of upper/lower case or because I forgot to put "[RemoteClass(alias="com.example.MyClass")]". Then I have 0.2% of casps that I can't get them to map, in these cases I simply do what Harbs said: create an object and manually map the properties.

Could you post the structure of your backend and the structure you have defined in as, maybe we can see something.

Hiedra.

-----Mensaje original-----
De: GAbe Barbosa <gb...@gmail.com> 
Enviado el: martes, 27 de abril de 2021 22:02
Para: dev@royale.apache.org
Asunto: Re: Casting workarounds

For clarity this is a cast that worked in Flex/Flash from the response of an AMF call. Upon investigation it looks like AMF is stripping away property class data on it's return from the server.

This is the structure. We return ResultVO from java with a property result of ChildVO.

In our Royale App we successfully cast var r:ResultVO = response as ResultVO;

but r.result is being seen as a Function() instead of ChildVO. So no matter if I cast with as operator or explicitly both fail.

Re: Casting workarounds

Posted by GAbe Barbosa <gb...@gmail.com>.
For clarity this is a cast that worked in Flex/Flash from the response of
an AMF call. Upon investigation it looks like AMF is stripping away
property class data on it's return from the server.

This is the structure. We return ResultVO from java with a property result
of ChildVO.

In our Royale App we successfully cast var r:ResultVO = response as
ResultVO;

but r.result is being seen as a Function() instead of ChildVO. So no matter
if I cast with as operator or explicitly both fail.

Re: Casting workarounds

Posted by Harbs <ha...@gmail.com>.
Huh. I didn’t remember that you did that.

There’s a JSONReviver class in Reflection and a JSON2ASVO example project which seems to demo how to use it.

Harbs

> On Apr 21, 2021, at 7:45 PM, Alex Harui <ah...@adobe.com.INVALID> wrote:
> 
> FWIW, IMO, it is a bit easier for data to be untyped in Royale than it was in Flex.
> 
> Flex was designed to support strongly-typed data coming from Flex Data Services, SOAP, and RemoteObject/AMF before REST-ful JSON services became popular.  Also the Flash runtime verifier was way more likely to catch implicit casts like in parameters to functions, so things were more likely to throw exceptions in the data layer in Flex.
> 
> I'm pretty sure I proved Royale can have a Reviver so that JSON.parse converts to typed objects.
> 
> But other code that is being migrated from Flex to Royale that expected a typed object may no longer get a typed object after the data layer is ported.
> 
> HTH,
> -Alex
> 
> On 4/21/21, 9:34 AM, "Josh Tynjala" <joshtynjala@bowlerhat.dev <ma...@bowlerhat.dev>> wrote:
> 
>    I guess Features and Concepts -> ActionScript 3 (AS3) would be the correct
>    category in the docs. Ideally, this category will eventually be filled with
>    many pages that teach a user how to use each of the language features of
>    AS3. So there should probably be a page that teaches how to cast an object
>    to a different type.
> 
>    At the bare minimum, the page about casting should also demonstrate the
>    syntax and explain how each type of cast behaves:
> 
>    ActionScript has two different types of casts.
> 
>    Function casts:
> 
>    var obj:MyType = MyType(anotherObj);
> 
>    As casts:
> 
>    var obj:MyType = anotherObj as MyType;
> 
>    Function casts throw an exception when the cast fails. As casts return null
>    when the cast fails.
> 
>    --
>    Josh Tynjala
>    Bowler Hat LLC <https://nam04.safelinks.protection.outlook.com/?url=https%3A%2F%2Fbowlerhat.dev%2F&amp;data=04%7C01%7Caharui%40adobe.com%7C4e8839a0c6594ce9db4508d904e351a0%7Cfa7b1b5a7b34438794aed2c178decee1%7C0%7C0%7C637546196639755635%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C1000&amp;sdata=L%2FLItyIefqFVoK%2FD8Naskku%2FovrZszTexkKdznHvL2U%3D&amp;reserved=0 <https://nam04.safelinks.protection.outlook.com/?url=https%3A%2F%2Fbowlerhat.dev%2F&amp;data=04%7C01%7Caharui%40adobe.com%7C4e8839a0c6594ce9db4508d904e351a0%7Cfa7b1b5a7b34438794aed2c178decee1%7C0%7C0%7C637546196639755635%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C1000&amp;sdata=L%2FLItyIefqFVoK%2FD8Naskku%2FovrZszTexkKdznHvL2U%3D&amp;reserved=0>>
> 
> 
>    On Wed, Apr 21, 2021 at 9:16 AM Andrew Wetmore <cottage14@gmail.com <ma...@gmail.com>> wrote:
> 
>> Where in the docs should I capture this wisdom?
>> 
>> On Wed., Apr. 21, 2021, 12:57 p.m. Josh Tynjala, <
>> joshtynjala@bowlerhat.dev <ma...@bowlerhat.dev>>
>> wrote:
>> 
>>> Just like how things worked in Flash, you can't just cast a random `var
>>> obj:Object = {}` to a class. It needs to be a real instance of that
>> class.
>>> The basic rule to keep in mind is this: You can use `as CustomVO` with
>>> something that was created with `new CustomVO()`. For any other type, an
>> as
>>> cast returns null.
>>> 
>>> --
>>> Josh Tynjala
>>> Bowler Hat LLC <https://nam04.safelinks.protection.outlook.com/?url=https%3A%2F%2Fbowlerhat.dev%2F&amp;data=04%7C01%7Caharui%40adobe.com%7C4e8839a0c6594ce9db4508d904e351a0%7Cfa7b1b5a7b34438794aed2c178decee1%7C0%7C0%7C637546196639755635%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C1000&amp;sdata=L%2FLItyIefqFVoK%2FD8Naskku%2FovrZszTexkKdznHvL2U%3D&amp;reserved=0 <https://nam04.safelinks.protection.outlook.com/?url=https%3A%2F%2Fbowlerhat.dev%2F&amp;data=04%7C01%7Caharui%40adobe.com%7C4e8839a0c6594ce9db4508d904e351a0%7Cfa7b1b5a7b34438794aed2c178decee1%7C0%7C0%7C637546196639755635%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C1000&amp;sdata=L%2FLItyIefqFVoK%2FD8Naskku%2FovrZszTexkKdznHvL2U%3D&amp;reserved=0>>
>>> 
>>> 
>>> On Tue, Apr 20, 2021 at 4:24 PM gbarbosa <gbarbosa333@gmail.com <ma...@gmail.com>> wrote:
>>> 
>>>> Hi Royale,
>>>> 
>>>> I was recently trying to cast an Object
>>>> 
>>>>    obj as CustomVO
>>>> 
>>>> but it would always turn to null. The as operator kept thinking
>> CustomVO
>>>> was
>>>> type Function() why would this be?
>>>> 
>>>> The workaround ending up being to set the prototype.
>>>> 
>>>>    Object.setPrototypeOf(obj, CustomVO.prototype);
>>>> 
>>>> and this successfully casted correctly. Is there a better way to do
>> this?
>>>> Most likely something something to do with the structure of CustomVO?
>> the
>>>> classes it extends?
>>>> 
>>>> Thanks for your time.
>>>> 
>>>> -Gabriel Barbosa
>>>> 
>>>> 
>>>> 
>>>> --
>>>> Sent from: https://nam04.safelinks.protection.outlook.com/?url=http%3A%2F%2Fapache-royale-development.20373.n8.nabble.com%2F&amp;data=04%7C01%7Caharui%40adobe.com%7C4e8839a0c6594ce9db4508d904e351a0%7Cfa7b1b5a7b34438794aed2c178decee1%7C0%7C0%7C637546196639755635%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C1000&amp;sdata=f0yhKv89bLx9OCfDw1Ten%2B6NzeibvhOiJPorbThvo74%3D&amp;reserved=0 <https://nam04.safelinks.protection.outlook.com/?url=http%3A%2F%2Fapache-royale-development.20373.n8.nabble.com%2F&amp;data=04%7C01%7Caharui%40adobe.com%7C4e8839a0c6594ce9db4508d904e351a0%7Cfa7b1b5a7b34438794aed2c178decee1%7C0%7C0%7C637546196639755635%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C1000&amp;sdata=f0yhKv89bLx9OCfDw1Ten%2B6NzeibvhOiJPorbThvo74%3D&amp;reserved=0>

Re: Casting workarounds

Posted by Alex Harui <ah...@adobe.com.INVALID>.
FWIW, IMO, it is a bit easier for data to be untyped in Royale than it was in Flex.

Flex was designed to support strongly-typed data coming from Flex Data Services, SOAP, and RemoteObject/AMF before REST-ful JSON services became popular.  Also the Flash runtime verifier was way more likely to catch implicit casts like in parameters to functions, so things were more likely to throw exceptions in the data layer in Flex.

I'm pretty sure I proved Royale can have a Reviver so that JSON.parse converts to typed objects.

But other code that is being migrated from Flex to Royale that expected a typed object may no longer get a typed object after the data layer is ported.

HTH,
-Alex

On 4/21/21, 9:34 AM, "Josh Tynjala" <jo...@bowlerhat.dev> wrote:

    I guess Features and Concepts -> ActionScript 3 (AS3) would be the correct
    category in the docs. Ideally, this category will eventually be filled with
    many pages that teach a user how to use each of the language features of
    AS3. So there should probably be a page that teaches how to cast an object
    to a different type.

    At the bare minimum, the page about casting should also demonstrate the
    syntax and explain how each type of cast behaves:

    ActionScript has two different types of casts.

    Function casts:

    var obj:MyType = MyType(anotherObj);

    As casts:

    var obj:MyType = anotherObj as MyType;

    Function casts throw an exception when the cast fails. As casts return null
    when the cast fails.

    --
    Josh Tynjala
    Bowler Hat LLC <https://nam04.safelinks.protection.outlook.com/?url=https%3A%2F%2Fbowlerhat.dev%2F&amp;data=04%7C01%7Caharui%40adobe.com%7C4e8839a0c6594ce9db4508d904e351a0%7Cfa7b1b5a7b34438794aed2c178decee1%7C0%7C0%7C637546196639755635%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C1000&amp;sdata=L%2FLItyIefqFVoK%2FD8Naskku%2FovrZszTexkKdznHvL2U%3D&amp;reserved=0>


    On Wed, Apr 21, 2021 at 9:16 AM Andrew Wetmore <co...@gmail.com> wrote:

    > Where in the docs should I capture this wisdom?
    >
    > On Wed., Apr. 21, 2021, 12:57 p.m. Josh Tynjala, <
    > joshtynjala@bowlerhat.dev>
    > wrote:
    >
    > > Just like how things worked in Flash, you can't just cast a random `var
    > > obj:Object = {}` to a class. It needs to be a real instance of that
    > class.
    > > The basic rule to keep in mind is this: You can use `as CustomVO` with
    > > something that was created with `new CustomVO()`. For any other type, an
    > as
    > > cast returns null.
    > >
    > > --
    > > Josh Tynjala
    > > Bowler Hat LLC <https://nam04.safelinks.protection.outlook.com/?url=https%3A%2F%2Fbowlerhat.dev%2F&amp;data=04%7C01%7Caharui%40adobe.com%7C4e8839a0c6594ce9db4508d904e351a0%7Cfa7b1b5a7b34438794aed2c178decee1%7C0%7C0%7C637546196639755635%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C1000&amp;sdata=L%2FLItyIefqFVoK%2FD8Naskku%2FovrZszTexkKdznHvL2U%3D&amp;reserved=0>
    > >
    > >
    > > On Tue, Apr 20, 2021 at 4:24 PM gbarbosa <gb...@gmail.com> wrote:
    > >
    > > > Hi Royale,
    > > >
    > > > I was recently trying to cast an Object
    > > >
    > > >     obj as CustomVO
    > > >
    > > > but it would always turn to null. The as operator kept thinking
    > CustomVO
    > > > was
    > > > type Function() why would this be?
    > > >
    > > > The workaround ending up being to set the prototype.
    > > >
    > > >     Object.setPrototypeOf(obj, CustomVO.prototype);
    > > >
    > > > and this successfully casted correctly. Is there a better way to do
    > this?
    > > > Most likely something something to do with the structure of CustomVO?
    > the
    > > > classes it extends?
    > > >
    > > > Thanks for your time.
    > > >
    > > > -Gabriel Barbosa
    > > >
    > > >
    > > >
    > > > --
    > > > Sent from: https://nam04.safelinks.protection.outlook.com/?url=http%3A%2F%2Fapache-royale-development.20373.n8.nabble.com%2F&amp;data=04%7C01%7Caharui%40adobe.com%7C4e8839a0c6594ce9db4508d904e351a0%7Cfa7b1b5a7b34438794aed2c178decee1%7C0%7C0%7C637546196639755635%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C1000&amp;sdata=f0yhKv89bLx9OCfDw1Ten%2B6NzeibvhOiJPorbThvo74%3D&amp;reserved=0
    > > >
    > >
    >


Re: Casting workarounds

Posted by Josh Tynjala <jo...@bowlerhat.dev>.
I guess Features and Concepts -> ActionScript 3 (AS3) would be the correct
category in the docs. Ideally, this category will eventually be filled with
many pages that teach a user how to use each of the language features of
AS3. So there should probably be a page that teaches how to cast an object
to a different type.

At the bare minimum, the page about casting should also demonstrate the
syntax and explain how each type of cast behaves:

ActionScript has two different types of casts.

Function casts:

var obj:MyType = MyType(anotherObj);

As casts:

var obj:MyType = anotherObj as MyType;

Function casts throw an exception when the cast fails. As casts return null
when the cast fails.

--
Josh Tynjala
Bowler Hat LLC <https://bowlerhat.dev>


On Wed, Apr 21, 2021 at 9:16 AM Andrew Wetmore <co...@gmail.com> wrote:

> Where in the docs should I capture this wisdom?
>
> On Wed., Apr. 21, 2021, 12:57 p.m. Josh Tynjala, <
> joshtynjala@bowlerhat.dev>
> wrote:
>
> > Just like how things worked in Flash, you can't just cast a random `var
> > obj:Object = {}` to a class. It needs to be a real instance of that
> class.
> > The basic rule to keep in mind is this: You can use `as CustomVO` with
> > something that was created with `new CustomVO()`. For any other type, an
> as
> > cast returns null.
> >
> > --
> > Josh Tynjala
> > Bowler Hat LLC <https://bowlerhat.dev>
> >
> >
> > On Tue, Apr 20, 2021 at 4:24 PM gbarbosa <gb...@gmail.com> wrote:
> >
> > > Hi Royale,
> > >
> > > I was recently trying to cast an Object
> > >
> > >     obj as CustomVO
> > >
> > > but it would always turn to null. The as operator kept thinking
> CustomVO
> > > was
> > > type Function() why would this be?
> > >
> > > The workaround ending up being to set the prototype.
> > >
> > >     Object.setPrototypeOf(obj, CustomVO.prototype);
> > >
> > > and this successfully casted correctly. Is there a better way to do
> this?
> > > Most likely something something to do with the structure of CustomVO?
> the
> > > classes it extends?
> > >
> > > Thanks for your time.
> > >
> > > -Gabriel Barbosa
> > >
> > >
> > >
> > > --
> > > Sent from: http://apache-royale-development.20373.n8.nabble.com/
> > >
> >
>

Re: Casting workarounds

Posted by Andrew Wetmore <co...@gmail.com>.
Where in the docs should I capture this wisdom?

On Wed., Apr. 21, 2021, 12:57 p.m. Josh Tynjala, <jo...@bowlerhat.dev>
wrote:

> Just like how things worked in Flash, you can't just cast a random `var
> obj:Object = {}` to a class. It needs to be a real instance of that class.
> The basic rule to keep in mind is this: You can use `as CustomVO` with
> something that was created with `new CustomVO()`. For any other type, an as
> cast returns null.
>
> --
> Josh Tynjala
> Bowler Hat LLC <https://bowlerhat.dev>
>
>
> On Tue, Apr 20, 2021 at 4:24 PM gbarbosa <gb...@gmail.com> wrote:
>
> > Hi Royale,
> >
> > I was recently trying to cast an Object
> >
> >     obj as CustomVO
> >
> > but it would always turn to null. The as operator kept thinking CustomVO
> > was
> > type Function() why would this be?
> >
> > The workaround ending up being to set the prototype.
> >
> >     Object.setPrototypeOf(obj, CustomVO.prototype);
> >
> > and this successfully casted correctly. Is there a better way to do this?
> > Most likely something something to do with the structure of CustomVO? the
> > classes it extends?
> >
> > Thanks for your time.
> >
> > -Gabriel Barbosa
> >
> >
> >
> > --
> > Sent from: http://apache-royale-development.20373.n8.nabble.com/
> >
>

Re: Casting workarounds

Posted by Josh Tynjala <jo...@bowlerhat.dev>.
Just like how things worked in Flash, you can't just cast a random `var
obj:Object = {}` to a class. It needs to be a real instance of that class.
The basic rule to keep in mind is this: You can use `as CustomVO` with
something that was created with `new CustomVO()`. For any other type, an as
cast returns null.

--
Josh Tynjala
Bowler Hat LLC <https://bowlerhat.dev>


On Tue, Apr 20, 2021 at 4:24 PM gbarbosa <gb...@gmail.com> wrote:

> Hi Royale,
>
> I was recently trying to cast an Object
>
>     obj as CustomVO
>
> but it would always turn to null. The as operator kept thinking CustomVO
> was
> type Function() why would this be?
>
> The workaround ending up being to set the prototype.
>
>     Object.setPrototypeOf(obj, CustomVO.prototype);
>
> and this successfully casted correctly. Is there a better way to do this?
> Most likely something something to do with the structure of CustomVO? the
> classes it extends?
>
> Thanks for your time.
>
> -Gabriel Barbosa
>
>
>
> --
> Sent from: http://apache-royale-development.20373.n8.nabble.com/
>

Re: Casting workarounds

Posted by Harbs <ha...@gmail.com>.
If you try casting an object which is not a CustomVO, you’ll get null.

I assume you are casting an object from JSON as a VO for type safety.

You can generally turn off as coercions for runtime so it won’t do that (something I generally do unless I actually want those coercions to save cycles at runtime). But doing so might cause weird minification issues.

I’d suggest actually converting those objects into proper VOs.

Something like this:
function CustomVO(obj:Object){
	this.prop1 = obj.prop1
	this.prop2 = obj.prop2
	etc...
}

I generally wrap the functionality of converting JSON in some kind of helper class so as soon as you get notification of a JSON response, it’s already converted.

HTH,
Harbs

> On Apr 21, 2021, at 2:24 AM, gbarbosa <gb...@gmail.com> wrote:
> 
> Hi Royale,
> 
> I was recently trying to cast an Object
> 
>    obj as CustomVO
> 
> but it would always turn to null. The as operator kept thinking CustomVO was
> type Function() why would this be?
> 
> The workaround ending up being to set the prototype.
> 
>    Object.setPrototypeOf(obj, CustomVO.prototype);
> 
> and this successfully casted correctly. Is there a better way to do this?
> Most likely something something to do with the structure of CustomVO? the
> classes it extends?
> 
> Thanks for your time.
> 
> -Gabriel Barbosa
> 
> 
> 
> --
> Sent from: http://apache-royale-development.20373.n8.nabble.com/