sjrd/scala-js-ts-importer

number[]|string[] type disjunction fails

Closed this issue · 9 comments

Hi, I'm trying to convert https://github.com/photonstorm/phaser/blob/master/typescript/phaser.d.ts to scalajs but fails in the line 58

``)'' expected but ErrorToken(illegal character) found
        constructor(game: Phaser.Game, parent: Phaser.Sprite, name: string, frameData: Phaser.FrameData, frames: number[]|string[], frameRate?: number, loop?: boolean);

the reason is pretty clear, scalajs doesnt support this type number[]|string[] ..not pretty sure but I think than it's named type disjunction no? :) .

how can handle it in scalajs?..thanks!!

sjrd commented

The recommended strategy is to turn these into several overloads of the method. One taking a number[] aka js.Array[Double], and one taking a string[] aka js.Array[String].

There are also properties that have two possible types. Maybe using Either would make sense.

sjrd commented

Using Either would not make sense, no. However the upcoming js.| will be perfect. (See scala-js/scala-js#1826)

this script is really great

https://github.com/GenslerAppsPod/scalavro/blob/master/util/src/main/scala/com/gensler/scalavro/util/union.scala

https://github.com/GenslerAppsPod/scalavro/blob/master/util/src/test/scala/com/gensler/scalavro/util/UnionSpec.scala

I replace the typescript union types with a type StringOrInt for instance, then I include the definition in the scala file and works great (it's really tricky although can be automatized), unfortunately I'm not using scalajs right now (but I love it) but this works ok for me, not sure but I think than the upcoming scala version will include union types, I remember seeing it in a slide

Adding to @cocodrino, I also want to parse the phaser.d.ts file, to ScalaJS.

I'd like to offer my 2 cents regarding the possible integration of Typescript's union types into Scala[JS], I came across the following Stackoverflow posts, outlining the use of Scala's 'Either' parameter marker or creating a group of 'Type Disjunction' (Union) helper objects/classes.

Using Scala's 'Either' for type selection

or

How to define “type disjunction” (union types) in Scala

The latter of these could be greatly aided by the use of Scala macros/quasiquotes to create new, as-yet-unamed, object-class relationships/unions (or any new Scala code tree).

Scala Macros/Quasiquotes

Also of interest might be the ability to compile new Scala code at runtime, using the 'Toolbox' api:

Just-in-time-scala-compilation

sjrd commented

We now have A | B in Scala.js. This is what should be used here.

That's great, apologies for being so long-winded, I just thought it best to offer as much help as possible. Of course, Scala.js's A | B construct is infinitely more simple/elegant.

Any ideas on when you might integrate this into you importer, thanks?

sjrd commented

In the coming weeks. I'll be busy until end of September, but after that I'll start working a bit on this.

sjrd commented

I just implemented this in the latest master. It is now possible to import phaser.d.ts.