I’ve the next perform which works:
package deal essential
kind CISettings interface {
Clone() CISettings
Merge(different CISettings)
}
kind Settings struct{}
func (s *Settings) Clone() CISettings {
return &Settings{}
}
func (s *Settings) Merge(different CISettings) {
}
func ParseCISettings[T CISettings](settings T) error {
sett := settings.Clone()
settings.Merge(sett)
return nil
}
func essential() {
sett := Settings{}
ParseCISettings[*Settings](&sett)
}
I’ve hassle discovering out why a signature the place I return the settings
doesn’t work:
ParseCISettings[T CISettings](settings T, err error)
I’m not grocking what the compiler trys to inform me. If I make worth receivers it really works, however thats not how Merge
ought to be applied, it wants a pointer receiver.
It is a quite simple interface drawback, in your code implementation, it’s ‘*Settings’ that implements ‘CISettings’, not ‘Settings’.
You’ll be able to strive calling it like this:
func essential() {
sett := Settings{}
ParseCISettings(&sett)
}
You understood the query improper:
I need to return a new kind Settings
not passing it in.
I don’t know what you’re making an attempt to precise, if you happen to can, write some pseudocode.
I’m nonetheless somewhat bit confused of you anticipated outcomes, however perhaps you are attempting to do one thing like this:
?
It tells you that you just can not use Settings
kind because the interface, as a result of this kind doesn’t implement this interface. The error is in essential perform right here:
ParseCISettings[Settings]()
You applied each strategies Clone and Merge for pointer: *Settings!
func (s *Settings) Clone() CISettings {
return &Settings{}
}
func (s *Settings) Merge(different CISettings) {}
You both want to vary it to worth itself or change the generic kind you’re passing in essential:
ParseCISettings[*Settings]()
Thanks, thats near what I would like.
However what ever the parse perform does I wished to return a price Settings
(or a pointer to it). However I dont know easy methods to assemble such a factor, trigger var v T
doesn’t work. Or how do I assemble a brand new worth in a generic perform utilizing T as parameter?
Are you able to please be extra particular on what precisely you are attempting to attain? What parameters the parsing perform ought to have, what it can return, as a result of now, I’m misplaced