elemental2: Native maps with primitive types?

410 views
Skip to first unread message

Vassilis Virvilis

unread,
Aug 14, 2019, 8:55:26 AM8/14/19
to google-we...@googlegroups.com
Hi,

Is it possible to define and use in java a javascript map (JsObject,
JsPropertyMap) that can accept plain numbers as the values of the map?

I didn't find a way. JsPropertyMap expect objects.

Thanks

--
Vassilis Virvilis

Jens

unread,
Aug 14, 2019, 10:38:11 AM8/14/19
to GWT Users
JsPropertyMap.set(key, value) is marked with @DoNotAutoBox, so I guess you should be able to use JsPropertyMap<Object> and set int, float, double into it without them being auto boxed.

-- J.

Vassilis Virvilis

unread,
Aug 15, 2019, 2:28:14 AM8/15/19
to google-we...@googlegroups.com
yep

that worked. thanks a bunch.

I have seen the @DoNotAutoBox and I tried with Integer, Number, JsNumber.

Note: I can put int in the JsPropertyMap but I cannot read it back. So
I cast it to (double) and the then to (int). I believe that is because
JavaScript does not have int type.

thanks again.


On Wed, Aug 14, 2019 at 5:38 PM Jens <jens.ne...@gmail.com> wrote:
>
> JsPropertyMap.set(key, value) is marked with @DoNotAutoBox, so I guess you should be able to use JsPropertyMap<Object> and set int, float, double into it without them being auto boxed.
>
> -- J.
>
> --
> You received this message because you are subscribed to the Google Groups "GWT Users" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to google-web-tool...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/google-web-toolkit/dd759896-4174-411d-b870-593054e1c660%40googlegroups.com.



--
Vassilis Virvilis

Peter Donald

unread,
Aug 15, 2019, 2:53:44 AM8/15/19
to GWT Mailing List
On Thu, Aug 15, 2019 at 4:28 PM Vassilis Virvilis <vas...@gmail.com> wrote:
I have seen the @DoNotAutoBox and I tried with Integer, Number, JsNumber.

Note: I can put int in the JsPropertyMap but I cannot read it back. So
I cast it to (double) and the then to (int). I believe that is because
JavaScript does not have int type.

That is certainly an option but in some circumstances can lead to type issues when casting. I tend to prefer using Any to do these sorts of conversions. It is a little more verbose but it avoids any unexpected casting problems. I don't have the code in front of me but I believe there is a method like getAsAny("myprop") that works in this scenario
 

--
Vassilis Virvilis


--
You received this message because you are subscribed to the Google Groups "GWT Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-web-tool...@googlegroups.com.

Vassilis Virvilis

unread,
Aug 16, 2019, 6:34:50 AM8/16/19
to google-we...@googlegroups.com
thanks that worked too.

It is Js.asAny(@DonotAutobox Object o) which performs an unchecked cast below.

Thanks for tips.
> To view this discussion on the web visit https://groups.google.com/d/msgid/google-web-toolkit/CACiKNc6CpmL2naFFydC%2BYS%3Dmtj3yQFsQ2KojGQCRAXXSRCYhLg%40mail.gmail.com.



--
Vassilis Virvilis
Reply all
Reply to author
Forward
0 new messages