I’m incredibly new at go and have been fiddling with generics. I’ve declared a function type that should take one type and convert it to another type. This works as expected.
type MapperFunction[TInput any, TOutput any] func(input TInput) TOutput
I have a struct Offshoot[T] and want to now create a receiver function that takes in this mapper func type. I cannot figure out how to pass the [TOutput] type to receiver function so the compiler knows what type to return from the mapper func. The below code is invalid and simply demonstrates the goal.
func (offshoot *Offshoot[TInput]) Mapper(mapper MapperFunction[TInput, TOutput]) Offshoot[TOutput] {
// call mapper and return new Offshoot[TOutput]
}
I’m able to declare the function as below, however this doesn’t work against an instance of Offshoot and requires passing it as a parameter.
func MapperT[TInput any, TOutput any](offshoot *Offshoot[TInput], mapper MapperFunction[TInput, TOutput]) Offshoot[TOutput] {