Skip to content

Support more types in switch conditions #648

New issue

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

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

Already on GitHub? # to your account

Closed
trusktr opened this issue Jun 10, 2019 · 4 comments · Fixed by #2926
Closed

Support more types in switch conditions #648

trusktr opened this issue Jun 10, 2019 · 4 comments · Fixed by #2926

Comments

@trusktr
Copy link
Member

trusktr commented Jun 10, 2019

I've got some variables all of the same type, and I'm trying to use them in a switch, something like:

const f: f64 = 1.3
const g: f64 = 2.4

switch(f) {
  case g: ...; break
}

but it result in an error like

ERROR AS200: Conversion from type 'f64' to 'u32' requires an explicit cast.
Here's the whole code:
	getHSL(target: HSL): HSL {
		// h,s,l ranges are in 0.0 - 1.0

		const r: f64 = this.r,
			g: f64 = this.g,
			b: f64 = this.b

		let max: f64 = Math.max(r, g)
		max = Math.max(max, b)

		let min: f64 = Math.max(r, g)
		min = Math.min(min, b)

		let hue: f64 = 0
		let saturation: f64 = 0
		const lightness: f64 = (min + max) / 2.0

		if (min === max) {
			hue = 0
			saturation = 0
		} else {
			const delta = max - min

			saturation = lightness <= 0.5 ? delta / (max + min) : delta / (2 - max - min)

			switch (max) {
				case r:
					hue = (g - b) / delta + (g < b ? 6 : 0)
					break
				case g:
					hue = (b - r) / delta + 2
					break
				case b:
					hue = (r - g) / delta + 4
					break
			}

			hue /= 6
		}

		target.h = hue
		target.s = saturation
		target.l = lightness

		return target
	}
and the error output:
ERROR AS200: Conversion from type 'f64' to 'u32' requires an explicit cast.

                        switch (max) {
            ~~~
 in src/as/glas/math/Color.ts(559,11)

ERROR AS200: Conversion from type 'f64' to 'u32' requires an explicit cast.

                                case r:
          ~
 in src/as/glas/math/Color.ts(560,9)

ERROR AS200: Conversion from type 'f64' to 'u32' requires an explicit cast.

                                case g:
          ~
 in src/as/glas/math/Color.ts(563,9)

ERROR AS200: Conversion from type 'f64' to 'u32' requires an explicit cast.

                                case b:
          ~
 in src/as/glas/math/Color.ts(566,9)
@willemneal
Copy link
Contributor

Hmm, I've never encountered switching on a floating point number before. I'm not sure it's well specified since floating point equality is not precise. I would switch to using if/elses.

@trusktr
Copy link
Member Author

trusktr commented Jun 13, 2019

That's what I ended up doing, but seems like the switch should use the same underlying equality check (probably using ==).

But even with conditionals, I'm having problems with the above code, I'm porting the code (and its tests) from JS to AS, but in AS I'm getting different values, and the tests are failing.

@MaxGraey
Copy link
Member

You could rewrite it to:

switch (true) {
  case max == r:
    hue = (g - b) / delta + (g < b ? 6 : 0)
    break
  case max == g:
    hue = (b - r) / delta + 2
    break
  case max == b:
    hue = (r - g) / delta + 4
    break
}

@stale stale bot added the stale label Jul 13, 2019
@stale stale bot removed the stale label Jul 13, 2019
@AssemblyScript AssemblyScript deleted a comment from stale bot Mar 16, 2020
@dcodeIO dcodeIO changed the title switch statement has unexpected type error Support more types in switch conditions May 28, 2020
@ColinEberhardt
Copy link
Contributor

+1 for supporting switch/case with string values

# for free to join this conversation on GitHub. Already have an account? # to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants