Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

feature: check exhaustiveness of type switches for sealed interfaces #74

Open
dkrieger opened this issue Feb 22, 2024 · 0 comments
Open

Comments

@dkrieger
Copy link

dkrieger commented Feb 22, 2024

sealed interfaces include a no-op private method so that only package-local types can implement the interface. it is therefore possible to check exhaustiveness statically.

example:

type Payload interface {
    sealed()
}

type FooPayload struct {
    Foo string
}

func (p FooPayload) sealed() {}

type BarPayload struct {
    Bar int
}

func (p BarPayload) sealed() {}

// . . .

switch p := somePayload.(type) {
case FooPayload:
    fmt.Println(p.Foo)
case BarPayload:
    fmt.Printf("%d", p.Bar)
}

see https://github.com/alecthomas/go-check-sumtype , which works but does not use analysis, and thus cannot be used with tools like nogo

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant