1

I have a function which currently doesn't receive a bool parameter, but then calls another function with a hardcoded bool. We need to remove the hardcoded call and allow a bool to be passed.

I first thought I could try some default parameter - my google searches resulted in that Go apparently doesn't support optional (resp. default) parameter.

So I thought I'd try function overloading.

I found this thread on reddit, which says that it works with a special directive since version 1.7.3: https://www.reddit.com/r/golang/comments/5c57kg/when_did_function_overloading_get_slipped_in/ I am using 1.8, and still I couldn't get it to work.

I am not even sure I may be allowed to use that directive, but I was speculating that changing the function signature right away may be dangerous as I don't know who uses the function...

Anyway - even with //+overloaded it didn't work

Is there any "idiosyncratic" way or pattern to solve this problem in Go?

//some comment
//+overloaded
func (self *RemoteSystem) Apply(rpath, lpath string, dynamic bool) error {
   result, err := anotherFunc(rpath, dynamic)  
}

//some comment
//+overloaded
func (self *RemoteSystem) Apply(rpath, lpath string ) error {
  //in this function anotherFunc was being called, and dynamic is hardcoded to true
   //result, err := anotherFunc(rpath, true)
  return self.Apply(rpath, lpath, true)
}

When I run my test, I get (forgive me for omitting part of the real path to file):

too many arguments in call to self.Apply
    have (string, string, bool)
    want (string, string)
../remotesystem.go:178: (*RemoteSystem).Apply redeclared in this block
    previous declaration at ../remotesystem.go:185
transient_loop
  • 5,984
  • 15
  • 58
  • 117
  • 4
    Go doesn't have function or method overloading. – JimB Mar 23 '17 at 20:14
  • Also check the first comment in the referenced reddit topic – zerkms Mar 23 '17 at 20:15
  • OK, so then there's no other way than changing the function signature? – transient_loop Mar 23 '17 at 20:15
  • 2
    Possible duplicate of [Does the Go language have function/method overloading?](http://stackoverflow.com/questions/6986944/does-the-go-language-have-function-method-overloading) Possible solution / alternative: [Optional Parameters?](http://stackoverflow.com/questions/2032149/optional-parameters) – icza Mar 23 '17 at 20:20

2 Answers2

6

Overloading isn't available in Go. Instead of writing functions with the same name that do different things, it is preferable to be more expressive with what the function does in the function name. In this instance, what would commonly be done is something like this:

func (self *RemoteSystem) Apply(rpath, lpath string, dynamic bool) error {
    result, err := anotherFunc(rpath, dynamic)  
}

func (self *RemoteSystem) ApplyDynamic(rpath, lpath string ) error {
    //in this function anotherFunc was being called, and dynamic is hardcoded to true
    return self.Apply(rpath, lpath, true)
}

Just by the name of the function, you can easily tell what is different and why.

Another example to provide some context (pun intended). I write a lot of Google App Engine code in Go using go-endpoints. The way to log things is different depending on if you have a context or not. My logging functions ended up like this.

func LogViaContext(c context.Context, m string, v ...interface{}) {
    if c != nil {
        appenginelog.Debugf(c, m, v...)
    }
}

func LogViaRequest(r *http.Request, m string, v ...interface{}) {
    if r != nil {
        c := appengine.NewContext(r)
        LogViaContext(c, m, v...)
    }
}
RayfenWindspear
  • 6,116
  • 1
  • 30
  • 42
3

From the Reddit post:

Unicode. I can tell by the pixels.

Go doesn't support function overloading. But it does support using Unicode characters in function names, which allows you to write function names that look like other function names.

The first one is setValue, the second one is setV\u0430lue aka setV\xd0\xb0lue (with CYRILLIC SMALL LETTER A) and the third is setVal\U0001d69ee aka setVal\xf0\x9d\x9a\x9ee (with MATHEMATICAL MONOSPACE SMALL U).

See also:

Community
  • 1
  • 1
Jonathon Reinhart
  • 132,704
  • 33
  • 254
  • 328