Declare function with type alias in Golang
Asked Answered
E

3

10

Is it possible to do something like this in Golang?

package main

import "fmt"

type myFunType func(x int) int

var myFun myFunType = myFunType { return x }  // (1) 

func doSomething(f myFunType) {
    fmt.Println(f(10))
}

func main() {
    doSomething(myFun)
}

In other words, is it possible to declare a function type variable using a function type alias, without repeating the signature? Or, alternatively, is there a way not to always retype the whole function-signature, whenever creating a variable of a function-type?

The above code sample, which I would expect to be equivalent to the one below (replace line (1) with line (2)), results in the compilation error syntax error: unexpected return, expecting expression.

package main

import "fmt"

type myFunType func(x int) int 

var myFun myFunType = func(x int) int { return 2 * x } // (2)

func doSomething(f myFunType) {
    fmt.Println(f(10))
}

func main() {
    doSomething(myFun)
}
Episcopacy answered 19/7, 2019 at 8:20 Comment(2)
Approaching this from a different angle: if you have a series of functions that all operate with the same signature and you're worried about having to type that over and over again, maybe it's time to encapsulate that into a struct and write methods that accept that struct as a receiver?Luanaluanda
You mean encapsulate the parameters of the function to a struct? Yeah, that would be a valid alternative.Episcopacy
E
8

From Spec: Function literals:

FunctionLit = "func" Signature FunctionBody .

A function literal must contain the func keyword and the Signature. Using a function type is not allowed by the syntax.

Same goes for Function declarations:

FunctionDecl = "func" FunctionName Signature [ FunctionBody ] .

Using a function type (instead of the signature) is not allowed.

So no, what you want is not possible. And the reason for it is because the signature (the function type) does not include the parameter names (just their order and types), but when you are actually "creating" a function value, you need a way to refer to them, and having just the function type you don't have names for the parameters.

See related questions for more details:

Getting method parameter names in Golang

Is unnamed arguments a thing in Go?

Eraste answered 19/7, 2019 at 8:26 Comment(0)
W
0

No, but in golang you can define methods with a name and use them.

As an example. Sometimes at the top of a file or even in a whole package there is a common way of defining errors like this:

ErrFileNotFound := func(file string) error { return errors.New(fmt.Sprintf("file not found %v", file))  }

And this function can then be used multiple times in the file like

file, err := os.Open(filenameRequestedToOpen) // For read access.
if err != nil {
  log.Fatal(ErrFileNotFound(filenameRequestedToOpen))
}

or see https://play.golang.org/p/CvBGGc3YeX4

Wrongly answered 28/9, 2020 at 7:27 Comment(0)
B
-1

var myFun myFunType = func(x int) int { return 2 * x } // (2) this is ok, myFun must contains func keyword,means it's a func type of myFunType, and parameters and return type must same with myFunType declared.

Babysitter answered 20/7, 2019 at 2:15 Comment(3)
Even if your answer is correct, it always a good idea to present it correctly :), like eloborate it a bit more and formating the code part.Advowson
Well, I know this is correct, but it does not help me, because what I wanted was not to repeat the signature of the function. I.e. the question was how to achieve (1).Episcopacy
myFunType contains func (x int) and return(int), so , var myFun myFunType = myFunType { return x } means func (x int) { return x} {return x}, i think. =..=Babysitter

© 2022 - 2024 — McMap. All rights reserved.