Friday, May 30, 2014

Re: Allow JavaScriptObjects to define custom implementation of instanceOf?



On Friday, May 30, 2014 4:12:03 PM UTC+2, Alexander Bertram wrote:
Hi all,

I came across an old design doc on the google code site discussing overlay types along with the following todo:
  • TODO: Consider whether or not a magic boolean allowCast(JavaScriptObject o) should be supported in each overlay type definition.

I've looked through the GWT jjs compiler source and it doesn't look this or anything else is implemented; the compiler will replace any instanceof's on JSO's with a cast without any further ado. 

This would be a super useful feature for our project because we have modeled our app's data as set of interfaces that each have a simple bean implementation for the JVM and 
JSO-based implementation for translatable code. 

Polymorphism is difficult there though because it's syntically legal to use instanceof to check for a subtype in both gwt and jvm code, it will fail silently on the client because all 
overlay types can be cast to another.

We resolve this by storing our "type" property, but it would be great to be able to provide an allowCast() method so that instanceof can be used everywhere.

I'd like to implement this, for our own project if nothing else, but was wondering if there had ever been a discussion about this or a deliberate decision against a "magic" allowCast. 
(or if anyone else has thought about implementing it?)

I'm not aware of a public discussion and/or decision about this, but I believe it's deliberate now that it doesn't exist (it'll be a bit different in the new/upcoming JsInterop https://docs.google.com/document/d/1Fsf3kSztERf_pFRj2ns1_qgQUiawsGMANKWa7jev20Q/edit?usp=drive_web but not that much)

What DOM Elements (in com.google.gwt.dom) do is that they define "boolean is(JavaScriptObject)" and "XxxElement as(JavaScriptObject)" static methods.
You'll note that they don't use the JS "instanceof" operator either (for cross-browser compat' in legacy browsers; see http://blog.calyptus.eu/seb/2009/11/the-performance-of-nodename/ apparently this could now be changed)

--
You received this message because you are subscribed to the Google Groups "Google Web Toolkit" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-web-toolkit+unsubscribe@googlegroups.com.
To post to this group, send email to google-web-toolkit@googlegroups.com.
Visit this group at http://groups.google.com/group/google-web-toolkit.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment