Skip to content

Instantly share code, notes, and snippets.

@networkimprov
Last active December 4, 2018 19:38
Show Gist options
  • Save networkimprov/7c1f311f26852bc912765e4110af062b to your computer and use it in GitHub Desktop.
Save networkimprov/7c1f311f26852bc912765e4110af062b to your computer and use it in GitHub Desktop.
Please Don't Mangle the Function Signature, re Go2 generics proposal

For gosh sakes, don't make us read code with the scrutiny of a compiler :-) A type declaration after a function name is confusingly similar to an argument list.

Which witch is which?

  • func Stuff(type T Something)(input T) error { ... }
  • func Stuffed(input Somethingelse) error { ... }

C++ style is fine; variations on paren placement can be considered

  • (type T, U) Something // line break or semicolon
    (type V) // more on next line
    func Stuff(input T, more U, other V) error { ... }

And consider simple function aliases for callers

  • func TypedStuff Stuff(TypeA, TypeB, TypeC)
    const TypedStuff = Stuff(TypeA, TypeB, TypeC) // alternatively
    err = TypedStuff(x, y, z)
@cblichmann
Copy link

I initially had the same issue after reading the proposal. However, there is a certain appeal in reusing the function call syntax.
If I had to change it, I'd votre for square brackets, same in the older drafts (placement notwithstanding):

  • func Stuff[type T Something](input T) error { ... }
  • func Stuffed(input Somethingelse) error { ... }

@win-t
Copy link

win-t commented Aug 29, 2018

I like func Stuff(type T Something)(input T) error { ... } because you can treat it like High-Order-Function

a := Stuff(int)
b := Stuff(bool)
a(10)
b(false)

@arendtio
Copy link

Well, my sentiment is a little harder: I like the 'simple' function signatures we have in Go today and don't like adding anything to them. Using the same bracket type twice is obviously making things harder to read and understand. So far we have

  1. func keyword
  2. type to bind function to (name and reference vs. value)
  3. function name
  4. input parameter list (name+type)
  5. return parameter list (mostly just types)
  6. start of function body (opening curly bracket)

Everything within one line. Looks complex enough for me. I hope we will end up with a solution which does not need any additions to the current function signatures. After all, our interfaces provide a similar complex functionality and they don't need any extra features within the function signatures either.

@rogpeppe
Copy link

I think the type keyword is a good clue that you've got a set of type parameters. I have to say that I think the syntax is really good - it doesn't introduce any new keywords or lexical tokens and it's quite readable and memorable. When writing some generics code using this syntax, I didn't get confused, and I think that's a good sign.

@networkimprov
Copy link
Author

networkimprov commented Aug 30, 2018

@rogpeppe, the trouble arises when you have a file of generic and non-generic functions. If it's all one or the other, your perception of text adjusts to that context.

Also a larger number of parameters and/or returns, each specified in the type list with a contract, becomes unwieldy for a single line.

@kirillx
Copy link

kirillx commented Sep 14, 2018

Absolutely agree, this kills the whole simplicity and readability of the language which made it efficient and so beloved.
If adding generics, then better implement as parametrised packages.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment