Hi

Is there a way to constrain one parameter to be assignable to another?

For example, in ye olde days, I'd have:

type Iface interface {
// ...
}

type Concrete1 struct { /* ... */ }
var _ Iface = &Concrete1{}

type Concrete2 struct{ /* ... */ }
var _ Iface = &Concrete2{}

// Repeat ad nauseam

Now, I'm finding myself wanting similar compile time checks for generic 
types and functions. E.g.

func foo[Iface any, Concrete Iface]() {
  var c Concrete
  var _ Iface = c
}

Obviously that's a bit contrived, but I'm writing code that needs both the 
concrete type (*someStruct) and the interface it's meant to adhere to.

The above code does not compile with 1.18 (cannot use a type parameter as 
constraint)

I have a runtime check that works, but I'd love a compile time check. Is 
there a way to do that?

Thanks
Michael



-- 
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+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/c78b9b90-a5e2-4acd-8026-a126805cb643n%40googlegroups.com.

Reply via email to