import "gonerics.io/d/set/int.git"
template "github.com/xxx/set"<int>
Basically, it's generics at the package level. The trick is neat :)
On Wed, Oct 1, 2014 at 9:26 AM, Jérôme Champion <cham...@gmail.com> wrote:Basically, it's generics at the package level. The trick is neat :)
I have a hunch a proper solution for generics in Go has to be at the package level. When I saw this, I immediately thought "this is an implementation of a weak form of Standard ML functors":when we write "package p" in a set of files, we create a new package. This package has a signature, which is the set of its exported symbols. When you import a package `import "../p"` the rule is that any name you refer as p.<N> has to be in the signature of the package. Otherwise, the compile will fail. Furthermore, the types of the signature has to match those of its uses.Now, any import will require a subset of the signature. Either all of the package, or a proper subset. The principle of substitution holds. If I writeimport p "pkg/path/foo"and subsequently use `p.N`, I can substitute this withimport p "pkg/path/bar"which is safe, provided the signature subset I use matches: foo and bar exports the same symbols with the same types for the subset I am using in my package. And modularity is all about this substitution principle. I can replace foo with bar as long as they have the same semantics.A functor is a function at the package level. That is, they are packages with "holes", much like a generic class is a class with "holes" into which we can fill details. So we can write:package intset = set(ints)package stringset = set(strings)for properly constructed packages set, ints, strings. The implementation would handle this by *expansion*, like in C++ templates or the MLton standard ML compiler. In practice, these expansions doesn't hurt much, experience shows.The problem with regard to compilation speed, however, can't be ignored. I do believe the added benefit of these kinds of constructions far outweigh the added compilation speed, but one has to acknowledge this isn't a flat compilation space anymore. Deeply nested "package compositions/functors" will lead to vastly increased compilation times. Also, it messes with separate compilation since you can't compile the set(·) file in advance unless your linker understands the inlining directives at the package level. Russ Cox's dictum rings true here:* Either you leave out this construction, slowing down the programmer* Or you implement composition of packages by indirection of reference, like Java, slowing down the implementation* Or you implement composition of packages by expansion, like MLton or C++ templates, slowing down the compiler
Apart from custom collections where else would I need generics for?
--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Now after about 18 months using Go I realized that most other languages have way too many useless features,corner cases that make code unreadable after 2 weeks (unreadable even by programmer who wrote it).It is bloatware parade out there.Go is light on the page. You can even read standard library and understand what is going on. Not that many languageshave such readability.
Perfection is achieved, not when there is nothing more to add, but when there is nothing left to take away.
Antoine de Saint-Exupery
func Add(a , b Number) Number {
return a+b
}
--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
It should definitely work locally, but a case might be made for splitting the code generator into a different kind of package, instead of living in the same one as the code that uses it.