Using variadic function / context in an interface?

107 views
Skip to first unread message

nimr...@gmail.com

unread,
Apr 26, 2018, 12:55:07 PM4/26/18
to golang-nuts
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?

Thanks a lot!
Nimrod.

simon place

unread,
Apr 26, 2018, 1:16:30 PM4/26/18
to golang-nuts
does the init NEED to be in the interface?

matthe...@gmail.com

unread,
Apr 27, 2018, 9:14:11 AM4/27/18
to golang-nuts
Each call to Run could check if the struct has been initialized and initialize it if not. An approach is a function field could be swapped out after initialization.

Matt

roger peppe

unread,
Apr 27, 2018, 9:28:10 AM4/27/18
to nimr...@gmail.com, golang-nuts
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.

nimr...@gmail.com

unread,
Apr 27, 2018, 10:15:47 AM4/27/18
to golang-nuts
I mean, it doesn't have to be I just find that it is more cleaner...
What happens now  - Init 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..

nimr...@gmail.com

unread,
Apr 27, 2018, 10:18:28 AM4/27/18
to golang-nuts
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..

Manlio Perillo

unread,
Apr 27, 2018, 11:54:49 AM4/27/18
to golang-nuts
You don't need an Init method.  Just make sure that the instance implementing the Runner interface is initialized before calling the Run method.

As an example:

package runner


type Runner interface {
  Run(x X, y Y)
}


type myrunner struct {
    // ...
}

func (r *runner) Run(x X,y Y) {
    // ...
}

func New(...) Runner {
    r = new(runner)
    // initialize the runner
    
    return r
}

Note that the runner type is not exported, so that the user can not accidentally call the Run method before the instance is initialized.
Also note that New returns Runner and not *myrunner.

Here is a working example:


Manlio
Reply all
Reply to author
Forward
0 new messages