cincheo / jsweet

A Java to JavaScript transpiler.
http://www.jsweet.org
Other
1.45k stars 160 forks source link

typeof support #45

Closed lgrignon closed 8 years ago

lgrignon commented 8 years ago

There is currently no equivalent of "typeof myvar" in JSweet, we could have an utility method, statically imported (like union / function / ...) and call String theType = typeof(myvar); in JSweet. It would also be possible to transpile getClass() to typeof but it doesn't feel like JSweet spirit.

Thanks.

ssatguru commented 8 years ago

until this is added what is the best way to do "typeof" in jsweet ?

renaudpawlak commented 8 years ago

Umm.... Right now I don't see a direct solution.... Since there is no workaround at this point, I will implement it tomorrow if possible. If you can, can you post your usecase?

renaudpawlak commented 8 years ago

I have implemented some support for instanceof and typeof. It came out pretty nice so far... See the documentation: https://github.com/cincheo/jsweet/blob/master/doc/jsweet-language-specifications.md#testing-the-type-of-an-object

renaudpawlak commented 8 years ago

For testing, don't forget to update to the latest snapshot (if necessary, clean up your local .m2 repo to start fresh).

renaudpawlak commented 8 years ago

update: I have moved the instanceof doc to the Semantics section because I felt that it was more appropriate. https://github.com/cincheo/jsweet/blob/master/doc/jsweet-language-specifications.md#testing-the-type-of-an-object-instanceof

ssatguru commented 8 years ago

That was quick :) Unfortunately not working out for me. Pulled in the latest core and transpiler

<groupId>org.jsweet.candies</groupId>
<artifactId>jsweet-core</artifactId>
<version>1.1.0-SNAPSHOT</version>

and

<groupId>org.jsweet</groupId>
<artifactId>jsweet-transpiler</artifactId>
<version>1.1.0-SNAPSHOT</version>

I can see it in the jsweet-core. But while compiling i get

[INFO] JSweet transpiler version 1.1.0-SNAPSHOT (build date: 2016-03-06 12:35:10)
...
ERROR: cannot find symbol
  symbol:   static typeof

Regarding use case I am developing a simple form generator. Based on the type of data, it generates various input element. Somehting similar to datgui my workaround right now is to use the eval function

String t = (String) jsweet.lang.Globals.eval("typeof snap[key]");
if ((t == "string") || (t == "number")) {
inp.type = "text";
...
} else if (t == "boolean") {
inp.type = "checkbox";
...
renaudpawlak commented 8 years ago

As usual, you are having really cool workarounds :) So, looking at your error, my best guess it that there is a dependendency you are using that hides the new typeof function. Older candies used to have the jsweet.util.Globals class bundled. It it not the case for newer candies. So you might want to check whether one of your candies is having jsweet.util.Globals class. Note that I am currently still trying to regenerate all the candies with this new format because there is another issue with candy generation that I am trying to fix in parallel.

Anyway, for simplicity, the fastest way to check this would be for you to send me the list of your Maven candy dependencies in you pom.xml (just copy-paste the dependencies section). I will be able to tell you very quickly if you are using a deprecated candy.

ssatguru commented 8 years ago

You were right. jquery, jqueryui and dat-gui candies had the jsweet.util.Globals in them I switched to the latest snapshots and that solved my issue.