Tuesday, April 22, 2025
HomeGolangGenerics on Reciver Strategies - Getting Assist

Generics on Reciver Strategies – Getting Assist


I’m conscious that generics can’t be used on receiver strategies. Subsequently I need to ask what you’d recommend for the next usecase: Say now we have consumer that may ship information.

kind Knowledge struct{
	content material string
}

kind Consumer struct {
}

func (consumer *Consumer) Ship(d Knowledge){
	// ...
}

func doit(){
	consumer := Consumer{}
	information := Knowledge{}
	consumer.Ship(information)
}

All tremendous to this point. Now we need to allow the consumer to ship information of a number of varieties. My first concept can be to make the information struct generic:

kind Knowledge[T any] struct{
	content material T
}

However this doesn’t work as a result of:

  • I can not use generic parameters in receiver strategies.
  • I can not kind the consumer because it shold have the ability to ship arbitary messages.

The one options I see are:

  • Dont use generics (specify a number of strategies, information kind as property on Knowledge struct, …)
  • Dont use receiver strategies.

However each options I might not prefer to miss.

What’s a proposed answer for that form of use case?

Test thi web page Generics facilitators in Go · rakyll.org



1 Like

Nice reply. Thanks very a lot.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments