Hey rog, Thanks for the reply! What happens now - I have an Init method for each struct implementing Runner, the Init method is not in the interface and I have to do type assertion every time I call to Init - That is for each class implementing the Runner interface. It's a bit ugly in my POV But I can live with it..
On Friday, April 27, 2018 at 4:28:10 PM UTC+3, rog wrote: > > On 26 April 2018 at 06:13, <nimr...@gmail.com <javascript:>> wrote: > > Hey everyone, > > I've encountered a design issue - > > I have an the following interface - > > > > type Runner interface { > > Run(x X,y Y) > > } > > > > I want to add to it an Init(...) method which will initialize the > internal > > fields of the Runner before performing `Run`.. > > My issue is that different structs implementing the `Runner` interface > > require different fields in order to initialize, How should I solve > this? > > The conventional solution to this would be to avoid putting Init in > the Runner interface; instead you could have different factory > functions that each return a new appropriately initialised Runner > instance. > > If you really need Init in there, one solution I've used in the past > is to have a uniform interface that involves initialization from > arbitrary data (in out case we used YAML, so each implementation knew > how to initialise itself from a YAML object). It all depends on the > constraints of your problem. > -- 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. For more options, visit https://groups.google.com/d/optout.