Working with opaque types (Char and Long)

核能气质少年 提交于 2019-12-05 09:37:05

Being opaque means that

  • There is no corresponding JavaScript type
  • There is no way to create a value of that type from JavaScript (except if there is an @JSExported constructor)
  • There is no way of manipulating a value of that type (other than calling @JSExported methods and fields)

It is still possible to receive a value of that type from Scala.js code, pass it around, and give it back to Scala.js code. It is also always possible to call .toString(), because java.lang.Object.toString() is @JSExported. Besides toString(), neither Char nor Long export anything, so you can't do anything else with them.

Hence, as you have experienced, a JavaScript 1 cannot be used as a Scala.js Long, because it's not of the right type. Neither is 'a' a valid Char (but it's a valid String).

Therefore, as you have inferred yourself, you must indeed avoid opaque types, and use other types instead if you need to create/manipulate them from JavaScript. The Scala.js side can convert back and forth using the standard tools in the language, such as someChar.toInt and someInt.toChar.

The choice of which type is best depends on your application. For Char, it could be Int or String. For Long, it could be String, a pair of Ints, or possibly even Double if the possible values never use more than 52 bits of precision.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!