Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create method with empty parenthesis #23

Merged
merged 1 commit into from Mar 16, 2015
Merged

Create method with empty parenthesis #23

merged 1 commit into from Mar 16, 2015

Conversation

eed3si9n
Copy link
Owner

Hi!

I'm using treehugger to generate wrapper code for an API in Scala.js.
One problem I'm facing is that when Scala.js encounters a method

def get: Int

it transforms this into something like

obj["get"]

whereas

def get(): Int

is correctly transformed into

obj["get"]()

(note the parenthesis at the end).
As empty parenthesis are also used in normal Scala to denote a method with side-effects, I was wondering if treehugger supports generating such methods.
Actually, the documentation contains an example where a method with empty parenthesis is generated, but I can't reproduce that locally.

If you point me in the right direction I can also try to create a PR for this.

Thanks!
Martin

@onsails
Copy link

onsails commented Mar 12, 2015

👍

@eed3si9n
Copy link
Owner

For a declaration (without implementation), DEF either needs to be converted implicitly into a tree, or you can call .tree method as follows:

import treehugger.forest._
import definitions._
import treehuggerDSL._

scala> val t = DEF("get", IntClass).tree
t: treehugger.forest.DefDef = DefDef(Modifiers(, , Map()),get,List(),List(List()),TypeTree(),EmptyTree)

scala> treeToString(t)
res0: String = def get: Int

@eed3si9n eed3si9n closed this Mar 16, 2015
@nightscape
Copy link
Author

Hi Eugene,

my problem is not that I want to create a DEF with an empty body.
I want to create a DEF with empty parenthesis, so I need

def get(): Int

instead of

def get: Int

This is due to a convention of Scala.js (see "Remarks" under http://www.scala-js.org/doc/calling-javascript.html) that states that methods without parenthesis are compiled to a property lookup and methods with parenthesis to a function call.

@eed3si9n
Copy link
Owner

Oh you actually want the parens. Sorry I got this other way around.

@eed3si9n eed3si9n reopened this Mar 16, 2015
eed3si9n added a commit that referenced this pull request Mar 16, 2015
Create method with empty parenthesis
@eed3si9n eed3si9n merged commit bb719a7 into master Mar 16, 2015
@eed3si9n eed3si9n deleted the topic/empty-paren branch March 16, 2015 22:01
@nightscape
Copy link
Author

Nice, exactly that :D

Thanks Eugene!

@eed3si9n
Copy link
Owner

0.4.1 is released with the change.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants