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 but not that much)

What DOM Elements (in 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 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
To post to this group, send email to
Visit this group at
For more options, visit

No comments:

Post a Comment