Are the two constraints equivalent to each other?

59 views
Skip to first unread message

tapi...@gmail.com

unread,
Apr 2, 2022, 9:50:13 AM4/2/22
to golang-nuts

package main

type Ptr[E any] *E

type MyConstraint interface {
    Ptr[int]
}

type YourConstraint interface {
    *int
}

func foo[T MyConstraint](x T) {
}

func bar[T YourConstraint](x T) {
}

func main() {
    var x = new(int)
    foo(x) // *int does not implement MyConstraint
    bar(x)
}

Axel Wagner

unread,
Apr 2, 2022, 10:00:27 AM4/2/22
to tapi...@gmail.com, golang-nuts
Ptr[int] is a defined type, `*int` is not. So the two types are not the same and this is working as intended.

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/e252f550-b5ae-43ca-b25d-12ce076fd74cn%40googlegroups.com.

tapi...@gmail.com

unread,
Apr 2, 2022, 10:12:13 AM4/2/22
to golang-nuts
Ah, I forgot that point.
Thanks for the explanation!
Reply all
Reply to author
Forward
0 new messages