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)
@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