Skip to content

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

@dkrieger

Description

@dkrieger

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions