cast Element to Elemental2

227 views
Skip to first unread message

Stefano Pulze

unread,
Oct 18, 2017, 4:43:18 AM10/18/17
to GWT Users
Hi all,

Can I cast "old" com.google.gwt.dom.client.Element to a new elemental2.dom.HTMLElement?

Thanks

Thomas Broyer

unread,
Oct 18, 2017, 7:39:58 AM10/18/17
to GWT Users
You should be able to cast a JSO to a JsType (native ones only?)
And in this case they indeed represent the same kind of objects so this is safe.

Vassilis Virvilis

unread,
Oct 18, 2017, 10:34:41 AM10/18/17
to google-we...@googlegroups.com
Thomas advice is of course preferable

But sometimes gwt type checking drives me crazy. Then I use this

JsUtil.java
----
   public static native <T, V> V cast(T data) /*-{
        return data;
    }-*/;
----

This casts anything to anything...

   Vassilis


On Wed, Oct 18, 2017 at 2:39 PM, Thomas Broyer <t.br...@gmail.com> wrote:
You should be able to cast a JSO to a JsType (native ones only?)
And in this case they indeed represent the same kind of objects so this is safe.

--
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-toolkit+unsub...@googlegroups.com.
To post to this group, send email to google-web-toolkit@googlegroups.com.
Visit this group at https://groups.google.com/group/google-web-toolkit.
For more options, visit https://groups.google.com/d/optout.



--
Vassilis Virvilis

Colin Alworth

unread,
Oct 18, 2017, 11:35:29 AM10/18/17
to GWT Users
That can still be done in JsInterop through the Js class - Js.cast does it safely, and Js.uncheckedCast does your JSNI version, but in a way that will continue to work correctly in J2CL.

The class also contains casts to all primitives.

If you are using elemental2, this should already be on your classpath, otherwise you can add it from maven using
 
   <dependency>
     
<groupId>com.google.jsinterop</groupId>
     
<artifactId>base</artifactId>
     
<version>1.0.0-beta-1</version>
   
</dependency>


On Wednesday, October 18, 2017 at 9:34:41 AM UTC-5, Vassilis Virvilis wrote:
Thomas advice is of course preferable

But sometimes gwt type checking drives me crazy. Then I use this

JsUtil.java
----
   public static native <T, V> V cast(T data) /*-{
        return data;
    }-*/;
----

This casts anything to anything...

   Vassilis

On Wed, Oct 18, 2017 at 2:39 PM, Thomas Broyer <t.br...@gmail.com> wrote:
You should be able to cast a JSO to a JsType (native ones only?)
And in this case they indeed represent the same kind of objects so this is safe.

--
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-toolkit+unsub...@googlegroups.com.
To post to this group, send email to google-we...@googlegroups.com.



--
Vassilis Virvilis

Max F

unread,
Oct 19, 2017, 3:14:17 AM10/19/17
to GWT Users
This is nice, I did not know this.

Would this be the preferred way of getting Elemental2 Context2d from calling getContext("2d")? Because the Elemental2 Context2d does'nt have Element in ancestry...
Or doing it as Brandon did:
CanvasRenderingContext2D
context = (CanvasRenderingContext2D) (Object) canvas.getContext("2d");?
In https://gist.github.com/branflake2267/8e4b9d4f2dc594fe21a125155516ec97

Kind regards,
Max
Reply all
Reply to author
Forward
0 new messages