veandco / go-sdl2-examples

This is where all go-sdl2 examples are stored
91 stars 33 forks source link

Compile error "impossible type switch case: sdl.QuitEvent" #15

Open chrplr opened 7 months ago

chrplr commented 7 months ago

When trying to compile examples/keyboard-input/keyboard-input.go, I get the following error:


impossible type switch case: sdl.QuitEvent event (variable of type sdl.Event) cannot have dynamic type sdl.QuitEvent

This is due to a change following the commit "examples: updated to remove pointer in event switch cases" where "*sdl.QuitEvent" was replaced by "sdl.QuitEvent" in the switch statement.

If I put back the dereferencing operator '*', the code compiles.

There must be a good reason for the change in the commit. Could you explain the rationale please? Thank you in advance !

If that matters, I am using go1.22.2 linux/amd64

veeableful commented 7 months ago

Hi @chrplr, the API was changed in v0.5.x so the example was updated as well. Could you try the examples referenced by v0.4.x?

chrplr commented 7 months ago

Ah! Sorry. Indeed. I copied the example directly from the current https://github.com/veandco/go-sdl2-examples and the go.mod file generated by the command go mod tidy on my computer contains

require github.com/veandco/go-sdl2 v0.4.38

I need to better understand how go mod tidy decides about the version...

Thanks a lot for go-sdl2 !

chrplr commented 7 months ago

Ah! Now, after running go get -v github.com/veandco/go-sdl2/sdl@master to obtain v0.5.0

my go.mod contains

require github.com/veandco/go-sdl2 v0.5.0-alpha.4.0.20240326043848-74189f852a83

but then compiling keyboard-input.go from examples yields:

./keyb.go:36:10: invalid case sdl.KMOD_LALT in switch on t.Keysym.Mod (mismatched types sdl.Keymod and uint16)
./keyb.go:38:10: invalid case sdl.KMOD_LCTRL in switch on t.Keysym.Mod (mismatched types sdl.Keymod and uint16)
./keyb.go:40:10: invalid case sdl.KMOD_LSHIFT in switch on t.Keysym.Mod (mismatched types sdl.Keymod and uint16)
./keyb.go:42:10: invalid case sdl.KMOD_LGUI in switch on t.Keysym.Mod (mismatched types sdl.Keymod and uint16)
./keyb.go:44:10: invalid case sdl.KMOD_RALT in switch on t.Keysym.Mod (mismatched types sdl.Keymod and uint16)
./keyb.go:46:10: invalid case sdl.KMOD_RCTRL in switch on t.Keysym.Mod (mismatched types sdl.Keymod and uint16)
./keyb.go:48:10: invalid case sdl.KMOD_RSHIFT in switch on t.Keysym.Mod (mismatched types sdl.Keymod and uint16)
./keyb.go:50:10: invalid case sdl.KMOD_RGUI in switch on t.Keysym.Mod (mismatched types sdl.Keymod and uint16)
./keyb.go:52:10: invalid case sdl.KMOD_NUM in switch on t.Keysym.Mod (mismatched types sdl.Keymod and uint16)
./keyb.go:54:10: invalid case sdl.KMOD_CAPS in switch on t.Keysym.Mod (mismatched types sdl.Keymod and uint16)
veeableful commented 7 months ago

Hi @chrplr, thanks for reporting it! The issue was in the v0.5.x code which I have fixed in the latest commit. You can use it by modiying the version in go.mod to 374428a92051 and then run go mod tidy.

iweave1 commented 2 months ago

Hi @chrplr, thanks for reporting it! The issue was in the v0.5.x code which I have fixed in the latest commit. You can use it by modiying the version in go.mod to 374428a92051 and then run go mod tidy.

Since go-sdl2 is still only on v0.4.0 release, I have to ask what are you talking about? I confirm on v0.4.0 the deference is required to compile.

veeableful commented 2 months ago

Hi @iweave1, I was referring to the master branch code which is to be v0.5.x.

iweave1 commented 2 months ago

Hi @iweave1, I was referring to the master branch code which is to be v0.5.x.

Thank you for clarifying!