Skip to content

Support more types in switch conditions #648

Closed

Description

@trusktr

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)

Activity

  1. willemneal commented on Jun 12, 2019

    @willemneal
    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.

  2. trusktr commented on Jun 13, 2019

    @trusktr
    MemberAuthor

    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.

  3. MaxGraey commented on Jun 13, 2019

    @MaxGraey
    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
    }
  4. deleted a comment from stale on Mar 16, 2020
  5. changed the title [-]switch statement has unexpected type error[/-] [+]Support more types in switch conditions[/+] on May 28, 2020
  6. ColinEberhardt commented on Dec 8, 2020

    @ColinEberhardt
    Contributor

    +1 for supporting switch/case with string values

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      ⌂ Proxy