3 ms·
Did you consider parameterized packages? The idea is you can declare a set of related objects/types/methods as well as have concrete type specific initializatio
by bakul 8y ago
Did you consider parameterized packages? The idea is you can declare a set of related objects/types/methods as well as have concrete type specific initialization. E.g.
package stack[t]
type Type []t
func New() Type { return Type{} }
...
Then it can be used so:
import s “stack”[int]
var s1 = s.New()
- pjmlp 8y agoJust as additional info to others, this is how Ada, Modula-2 and Modula-3 generics kind of work.
- weberc2 8y agoI’ve seen this in ocaml and always thought it strange. I think it’s useful, but binding it to a package seems odd since packages are units of code distribution or some such.
- nickcw 8y agoI think parameterized packages are a great idea. It would be a light-weight way of getting just a bit of generic code into go. I made gotemplate to explore that idea https://github.com/ncw/gotemplate https://github.com/ncw/gotemplate This requires a round of `go generate` for the actual code generation, but otherwise quite a similar experience. Having it built in would be great!
- polymathist 8y agoParameterized packages are interesting but they come with their own problems. It can get tedious if I want to use, e.g., a stack with multiple different types. They also lose some of the flexibility that comes with scoping type parameters to any function/method/data structure.
- bakul 8y agoOn the other hand writing such packages becomes easier as only two syntax extensions are needed (package header and import). You can easily abstract a package by replacing a concrete type with a package level parameter. More importantly, there is no other mechanism to tie together a bunch of objects, functions, methods etc. to the same parameter types.