Khan / Prototope

Swift library of lightweight interfaces for prototyping, bridged to JS
http://khan.github.io/Prototope
230 stars 18 forks source link

POC: Wrap bridged types to throw when called as a function #47

Open sophiebits opened 9 years ago

sophiebits commented 9 years ago

For #25: This shows how we might wrap the constructors so that they throw when called without new. In this example, I've hardcoded TextLayer as the only wrapped type, but we'd presumably want to wrap every instantiable type. When you call a type without new, you now get a protonope, as you might expect. (It always says line 5 though since that's where the new Error() call is in the wrap script -- not sure how to fix that easily.)

image

sophiebits commented 9 years ago

We could also prohibit new and do it implicitly if we wanted to make the code feel Swift-ier. Lots of JS libraries actually do the implicit instantiation thing.

saniul commented 9 years ago

Looks great! Thanks, Ben!

I would vote for doing the implicit jnstantiation on user's behalf, since this is obviously what the user wanted to do anyway.

andymatuschak commented 9 years ago

Ahaha way cool, @spicyj! Will dig into this in the next couple days (we're in NYC now for early math stuff hiiiiiiii)

andymatuschak commented 9 years ago

My preference would be to throw an exception (as Ben's done) if the user leaves off the new. I don't think it's a good idea to create variant dialects of JS—people using Prototope-JS should be able to refer to ordinary (and widely-available) JS resources to understand their environment.

@nsfmc? @jbrennan? Have opinions on that?

I'll get to work on productionizing this. How does "commandeering" work on GH? Do I just open up another PR?

saniul commented 9 years ago

Yup, another PR works

Sent from my iPhone

On 10 Mar 2015, at 06:12, Andy Matuschak notifications@github.com wrote:

My preference would be to throw an exception (as Ben's done) if the user leaves off the new. I don't think it's a good idea to create variant dialects of JS—people using Prototope-JS should be able to refer to ordinary (and widely-available) JS resources to understand their environment.

@nsfmc? @jbrennan? Have opinions on that?

I'll get to work on productionizing this. How does "commandeering" work on GH? Do I just open up another PR?

— Reply to this email directly or view it on GitHub.

jbrennan commented 9 years ago

My preference would be to throw an exception (as Ben's done) if the user leaves off the new.

+1

sophiebits commented 9 years ago

As a JS person (but not a Prototope person), I would not find it out of place to have these constructors not use new at all. Lots of popular JS libraries have "constructors" that you call without new. Like jQuery has you write var foo = $.Deferred(); to create a promise-like object. I think my personal preference is to forbid new.

sophiebits commented 9 years ago

(Yeah, just open a new PR.)

saniul commented 9 years ago

Relevant: http://jfire.io/blog/2013/03/20/newless-javascript/

andymatuschak commented 9 years ago

(actually, as awesome as it is that @spicyj has sent us a prototype, integrating it while maintaining DRY will require some work; deprioritizing)

andymatuschak commented 9 years ago

I did a bunch of the work here: https://github.com/Khan/Prototope/tree/pr/47