With the given type signature below, is there a way to do something similar to below?
func Transform[T, U any](item T) U {
return item
}
The code above gives the following error:
cannot use item (variable of type T constrained by any) as U value in return
statement
I am unable to use the type signature above, as I am essentially trying to make an optional transform method that sometimes will need to convert from T to U, but sometimes just return itself. A more detailed example of the use case is shown below.
type SomeStruct[T, U any] struct {
Transform func(T) U
}
func (s SomeStruct[T, U]) Transform(elem T) (U) {
if s.Transform != nil {
return s.Transform(elem)
}
return elem
}
Is there a way to create a Transform function that sometimes conditionally just returns itself?