7 ms·
Function types in Go
- sirclueless 14y agoI really liked this treatment of first-class functions. It started off slow, with a lot of trivial, contrived examples that should be obvious to anyone who has ever seen first-class functions in any language. But then there were some very pragmatic patterns, with standard library examples, that used recursive function types and methods on function types to build some very neat APIs. Building a lexer out of a recursive function type actually sounds like a really cool thing to try, if I find some free time I want to try it without looking at Go's standard library `text/template` lexer and see how it compares.
- jbarham 14y ago> Building a lexer out of a recursive function type actually sounds like a really cool thing to try Rob Pike did just that for Go's text/template package (http://golang.org/pkg/text/template/ http://golang.org/pkg/text/template/). See the source lexer source code at http://golang.org/src/pkg/text/template/parse/lex.go http://golang.org/src/pkg/text/template/parse/lex.go and note that stateFn is defined recursively. There's a video by Pike describing how he built the parser at http://www.youtube.com/watch?v=HxaD_trXwRE http://www.youtube.com/watch?v=HxaD_trXwRE.
- quarterto 14y agoI never really noticed how similar Go's function expressions are to JavaScript's before, but I'm now pretty certain they were one of the (subconscious) factors that got me into Go.
- jeremyjh 14y agoGo's support for first-class functions definitely sparked my interest in the language. But one thing that Javascript, Python and Ruby programmers would expect of higher order function support is the ability to use the general collection methods such as map, reduce, filter (or inject, collect, fold - whatever they are called in your language). So far as I can tell, generalized functions of these types are not really possible. You could write some that were typed interface{} and then did explicit casts in the closures you passed, but that would not be nearly as elegant as what other languages provide. I believe in a static type system without generics this is not really possible, and this is really (to me) the most painful cost from the lack of generic support.
- koenigdavidmj 14y agoGo is an opinionated language. Go people would just tell you to write the loop; that's what it's there for.
- jlgreco 14y agoTo be clear, generics seems to be one thing that Go isn't actually opinionated on. As I understand it, they are open to including generics in future versions of the language, but only if the implementation is elegant.
- _ak 14y agoWell, as far as I understand, that implementation would need to overcome the Generic Dilemma as described by Russ Cox: http://research.swtch.com/generic http://research.swtch.com/generic So far, the Go team decided to go for approach #1.
- maaku 14y agoOption #3 is unfairly presented. There is no reason a language couldn't implicitly box every type, but automatically create efficient specializations for (some of) the types actually used. You can have your cake and eat it too. Of course, it's not easy to write such static optimizations. But it is, in principle, possible, and the sort of thing you only need write once.
- dualogy 14y ago> But it is, in principle, possible, and the sort of thing you only need write once. Well Go is open source so "someone" would just have to write, test and submit a CL / patch... ;-)
- masklinn 14y ago> So far, the Go team decided to go for approach #1. Except where they personally really wanted parametric polymorphic collections so they baked them as special case into the runtime, of course (map, chan, …)
- pjonesdotca 14y agoProbably my favourite thing about Go. The ability to create a DSL with a systems level language is kind of amazing.
- mieubrisse 14y agoAlways fun to see examples of first-class functions; thank you for the read!
- melvinmt 14y agoAnonymous functions as an expression do have their purpose, unlike the author has stated, especially when you want to isolate different blocks of code from each other by putting them each in a different scope.
- dualogy 14y ago"Function types in Go can also have methods." Wow, blew my mind... this has never occurred to me but, of course, why not? Not sure yet how I am going to abuse that feature, but already looking forward to it.
- dsymonds 14y agoThe HandlerFunc type in net/http (http://golang.org/pkg/net/http/#HandlerFunc http://golang.org/pkg/net/http/#HandlerFunc) does this.
- zemo 14y agochannel types can have methods, too ;) http://gowithconfidence.tumblr.com/post/31426832143/stacked-channels http://gowithconfidence.tumblr.com/post/31426832143/stacked-...