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

Type error in type assertions #793

Open
HSMF opened this issue Nov 2, 2024 · 0 comments
Open

Type error in type assertions #793

HSMF opened this issue Nov 2, 2024 · 0 comments

Comments

@HSMF
Copy link

HSMF commented Nov 2, 2024

The following snippet fails in the type checker.

package main

type T interface {
        Foo()
}

func compute(e T) int {
        v, ok := e.(interface {
                F() int
        })

        if ok {
                return v.F()
        } else {
                return 0
        }
}

The error seems to be that in order to type check x.(T), where x is of type S the type checker first checks that T implements S.

Since in the above snippet, v is only used if ok, it makes sense to allow the type assertion to fail.

12:27:40.434 [thread-4] ERROR viper.gobra.reporting.FileWriterReporter - Error at: <main.go:8:11> main.go:8:11:error: type error: type interface{ F } does not implement the interface T
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