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

Export inner objects in Scala classes #4142

Open
swachter opened this issue Jul 29, 2020 · 4 comments
Open

Export inner objects in Scala classes #4142

swachter opened this issue Jul 29, 2020 · 4 comments

Comments

@swachter
Copy link

@swachter swachter commented Jul 29, 2020

In the following example the innerObj field is not set, i.e. new ClassWithInnerObj().innerObj is undefined:

@JSExportTopLevel("ClassWithInnerObj")
@JSExportAll
class ClassWithInnerObj {
  val x = 1
  object innerObj extends js.Object {
    val y = 2
  }
}

The generated code contains only an assignment of null to the field. No other references exist in the generated code:

   this.LClassWithInnerObj__f_innerObj$module = null;
@sjrd sjrd changed the title Support inner objects in SJS classes Export inner objects in Scala classes Jul 29, 2020
@sjrd
Copy link
Member

@sjrd sjrd commented Jul 29, 2020

Thanks for opening the issue. Could you also post here what happens when adding @JSExport directly on object innerObj, because that would be the root cause. @JSExportAll is merely a shortcut for many @JSExports.

@swachter
Copy link
Author

@swachter swachter commented Jul 29, 2020

The compiler complains @JSExport is forbidden on objects and classes.

@sjrd
Copy link
Member

@sjrd sjrd commented Jul 29, 2020

As a workaround, it's possible to export a getter for the object instead:

class ClassWithInnerObj {
  val x = 1

  @JSExport
  def innerObj: innerObjInternal.type = innerObjInternal

  object innerObjInternal extends js.Object {
    val y = 2
  }
}
@swachter
Copy link
Author

@swachter swachter commented Jul 31, 2020

Thanks for the workaround. I use it in the adapter code generation of the ScalaTsPlugin. After this is fixed I would like to get rid of the workaround because it causes two members to be created in the TypeScript declaration file. There is the danger to access the 'object field' directly instead of the assigned value.

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

Successfully merging a pull request may close this issue.

None yet
2 participants
You can’t perform that action at this time.