Proposal for making Object literals more intuitive to define

164 views
Skip to first unread message

Paul Stockley

unread,
Apr 6, 2016, 9:03:20 AM4/6/16
to GWT Contributors
Would it be possible add support for JsType(literal=true). I am not proposing this would work according to the future object literal spec. It would just be the equivalent of JsType(isNative=true, namespace=JsPackage.GLOBAL, name="Object), I think this would make the code a lot more intuitive to follow and express the intention more clearly.

Thomas Broyer

unread,
Apr 6, 2016, 9:05:49 AM4/6/16
to GWT Contributors
This has nothing to do with an "Object literal".

Paul Stockley

unread,
Apr 6, 2016, 11:46:49 AM4/6/16
to GWT Contributors
This is the only way you can create a true object literal equivalent in JsInterop (i.e. no prototype other than Object) 

The following is as close as you can get to say { prop1 : 1 }

JsType(isNative=true, namespace=JsPackage.GLOBAL, name="Object")
class SomeLiteral() {
   int prop1
}

SomeLiteral o= new SomeLiteral ();

o.prop1 = 1;

Thomas Broyer

unread,
Apr 7, 2016, 6:51:06 AM4/7/16
to GWT Contributors


On Wednesday, April 6, 2016 at 5:46:49 PM UTC+2, Paul Stockley wrote:
This is the only way you can create a true object literal equivalent in JsInterop (i.e. no prototype other than Object) 

The real question is whether (and why) you need "no prototype other than Object", other than for the bloat in the generated JS (and then @JsType(isNative=true, namespace=JsPackage.GLOBAL, name="Object") is what you're after, no need for syntactic sugar –moreover if it has approximate naming semantics)
Plus: your proposed literal=true as a synonym for isNative=true,namespace=JsPackage.GLOBAL,name="Object", in addition to being misleading, is likely to collide with future proper object literal support, at least as currently proposed in https://docs.google.com/document/d/1DFrC-GtcK7cu6DGxaWCswvb2fai9cnrWPvGcdgsKlBw/edit?usp=sharing (I, for one, quite like the JsStruct; and I'd go as far as making it a special type, without the @JsType(literal=true) at all)

Paul Stockley

unread,
Apr 7, 2016, 9:14:28 AM4/7/16
to GWT Contributors
Bloat and performance is one reason. However, the real reason is that frameworks like react and redux choke if you pass anything but a plain JavaScript object to some of their API's. I don't really care what the annotation is e.g. JsStruct or JsLiteral would work just as well.

Brandon Donnelson

unread,
Apr 7, 2016, 12:26:25 PM4/7/16
to GWT Contributors
Using what Thomas said works good. I've had the same question about using Object literals.  So I wrote down some examples and stored them here. You may need 2.8 +(nightly) to use some of the example options.
Reply all
Reply to author
Forward
0 new messages