News for hackers. Stuff that matters.
Re:Why I’m not leaving Python for Go by tikhonj on Sunday September 23, 2012 @10:44PM (#4562964)
As usual in these threads about Go, I really wish people would consider Haskell as a nice alternative. A lot of people write Haskell off as "academic" or "impractical", which I feel is not an entirely fair assessment.

Particularly: Haskell is fast, concurrent by design (whatever that means, I'm sure Haskell is :P), typed but not cumbersome or ugly (less cumbersome and ugly than Go's types, even) and--most importantly for this article--does error handling really well.

In a certain sense, Haskell's main method for handling errors is actually similar to Go's. It returns a type corresponding to either an error or a value (this type, naturally, is called Either). This method is special, oddly enough, because it isn't special: Either is just a normal Haskell type so the error checking isn't baked into the language.

Coming from another language, you would expect to have to check your return value each time. That is, you fear your code would look like this pseudocode:

    if isError val1 
      then pass val1
      else val2 <- someFunction val1
           if isError val2
           ...
However, this is not the case! The people implementing Either noticed that this was a very common case: most of the time, you want to propagate an error value outwards and only do any work on a valid value. So you can actually just write code like this:

    do val1 <- someValue
       val2 <- someFunction val1
       val3 <- someFunction val2
       someFunction val3
then, if any of the values return an error, it gets propagated to the end of the code. Then, when you want to check if you actually got a valid value--maybe in some central location in your code, or wherever is convenient--you can just use a normal case analysis.

Additionally, while the errors do pass through essentially silently, the type checker does ensure you deal with them at some point before using or returning them. If you ever want to get the Int from a Either Error Int, you have to either handle the error case somehow or explicitly ignore it (e.g. with a partial pattern match). The latter option will generate a compiler warning, so you can't do it by accident without being notified.

So the mechanism is simple, but it can also stay out of your way syntactically. So what else is this good for? Well, it's just a normal data type, nothing special; you can use existing library functions with these values. For example, you can use the alternation operator <|> to find the first non-error value:

    val1 <|> val2 <|> someFunction 5 <|> ...
This is often a very useful idiom which would be harder to write with a different way of handling errors. There are more utility functions like this (e.g. optional) that let you make your intents very clear at a high level. The optional function, for example, lets you do exactly what it claims: you can mark a value as "optional", meaning that any error from it will be ignored rather than propagated.

You can also layer on this error-handling logic on other similar effects. For example, there are some types (like LogicT) that represent backtracking search. Combining error-handling with nondeterminism gives you a question: should an error cause the whole computation to fail, or only that particular branch? The beauty is that you can choose either option: if you wrap LogicT with ErrorT (this is just like Either except it can be combined with other types) an error will cause the whole computation to fail; if you wrap ErrorT with LogicT, the error will only cause the current branch to fail. This not only makes it easy to choose one or the other, but also makes it very clear which one you did choose: it's right there in the type system in a very declarative fashion.

Haskell also has a bunch of other advantages which aren't worth going into here. I think anybody looking for something like Go--a fast, high-level, concise, typed alternative to Python--should definitely consider Haskell. While it may not seem so at first, I believe that Go and Haskell are good for a very similar set of problems and so actually overlap quite a bit.

If you really dislike some particular things about Haskell, you should also consider some similar languages like OCaml and F#. I personally prefer Haskell, but there are definitely good cases to be made for either of the other two.

Re:Why I’m not leaving Python for Go by cletus on Monday September 24, 2012 @01:42AM (#4563370)
This kind of comment kinda misses the point.

Haskell (IMHO) won't ever be mainstream for much the same reasons Lisp never was (or will be?): it has an incredibly high learning curve (eg [1]).

This is really the problem of the "pure" functional languages. Functional programming is suited to some tasks. With others the fit is almost tortuous. Imperative programming is well-suited to how we think and how we break down tasks.

If you look at the popular languages they're pretty much all multi-paradigm (eg Ruby, Python, Go, C#, arguably even C++/Java) meaning they give you the low-hanging fruit of functional programming while still being in a relative sweet spot of being easy to learn yet reasonably expressive.

Why I think Go has a very bright future ahead of it is that it is the first of these multi-paradigm language to combine all these features:

- easy to learn (seriously, go look at how short [2] is; you can knock that out in an afternoon);

- it is minimal. I LOVE Go's minimal OO model for example; and

- it is statically typed.

If you look at the other statically typed multi-paradigm languages you have C#, which is generally well-regarded... except (Mono notwithstanding) it is very Windows-centric. Java is, well, Java. C++ is incredibly complicated.

The only thing Go is missing is a mode for (semi-)manual memory management. I'm thinking something like Obj-C's ARC (in iOS 5+) and it could well supplant C/C++ for the vast majority of their (already shriking) use cases... eventually (Go has some work to do on speed).

[1]: http://stackoverflow.com/questions/377082/how-long-does-it-t...

[2]: http://www.miek.nl/projects/learninggo/

Re:Why I’m not leaving Python for Go by cageface on Monday September 24, 2012 @02:08AM (#4563421)
Haskell (IMHO) won't ever be mainstream for much the same reasons Lisp never was (or will be?): it has an incredibly high learning curve (eg [1]).

Bingo. Anybody that thinks Haskell will ever escape its niche has not spent enough time working with rank & file programmers. That doesn't mean you shouldn't consider it or that it can't find a large enough niche to sustain itself but it does mean that it's unlikely to ever overcome what Carmack refers to as "externalities" for a lot of prospective users.

Re:Why I’m not leaving Python for Go by theaeolist on Monday September 24, 2012 @03:25AM (#4563587)
This kind of comment kind of misses the point. The monadic style of threading error values is perfectly compatible with imperative programming if only language designers knew about it.
Re:Why I’m not leaving Python for Go by nostrademons on Monday September 24, 2012 @03:50AM (#4563647)
Problem is that you then have to explain monads to average programmers who'll be using the language.

I love monads (I love arrows more, but that's another issue). I'm a language design geek. My level of expertise is different from someone who has just been hired into a new job and wants to get things done.

I suspect that most language designers know about monads - I'm not sure Rob Pike did, but it's pretty common knowledge among folks who do this stuff. Until you can explain them to the folks who'll be using the language in a way that doesn't make them seem like arcane black magic, though, you'll find programmers will just say "I don't get this" and use what they're familiar with.

Re:Why I’m not leaving Python for Go by flyinRyan on Monday September 24, 2012 @07:32AM (#4564090)
>Problem is that you then have to explain monads to average programmers who'll be using the language.

No you don't, this is exactly what you don't need to do. Do we explain the finer points of stream implementation to would-be C++ programmers? No, when they need to do something non-standard with a stream/monad, that's the time to talk about them. For the majority of programmers it's just "when you need to write to a file you do it like this".

Re:Why I’m not leaving Python for Go by morphyn on Monday September 24, 2012 @09:50AM (#4564629)
The Either type described in the first comment is also a Monad. They are more important in Haskell than just "when you need to write to a file..." and I don't think you can totally grasp the error handling method without understanding them.
Re:Why I’m not leaving Python for Go by flyinRyan on Monday September 24, 2012 @10:47AM (#4564918)
I couldn't disagree more. The Either type may have a Monad interface, but it's a Sum type and languages that don't have thousands of Monad tutorials also have Sum types. There's no need to explain Monad theory to someone just to explain Sum types or even why chaining do expressions doesn't do unnecessary computations in the face of errors. Just show them the code for join and >> and they'll see why it works. No need to bring up Monads.
Re:Why I’m not leaving Python for Go by Evbn on Monday September 24, 2012 @11:19PM (#4568379)
I don't know what a thread is, but I can still use Executors in Java.
Re:Why I’m not leaving Python for Go by Evbn on Monday September 24, 2012 @11:22PM (#4568389)
You can write Haskell programs entirely in IO, and the result looks and works almost exactly like Python, but type safe.

The dangerous part of Haskell, like Scala and C++, is that it also has super scary dark magical parts.

Re:Why I’m not leaving Python for Go by _ak on Monday September 24, 2012 @08:46AM (#4564333)
> As usual in these threads about Go, I really wish people would consider Haskell as a nice alternative. A lot of people write Haskell off as "academic" or "impractical", which I feel is not an entirely fair assessment.

As someone with 10+ years of programming experience in the industry but no formal college education, I see the problem with Haskell (and other, similar functional programming languages) that in order to fully understand the language, you need to understand its theoretical foundation. Same goes for monads, combinators, etc. Without understanding the theory they're based on, it's impossible to use them and also hard to read programs that employ them.

The theoretical foundation of Go on the other hand is much smaller, and way easier to understand, especially for people without a formal CS education.

So, yes, I admit that I don't "get" Haskell in all its glory, and I'm not ashamed of it because I know that most people in IT don't and that's why it will always remain relatively obscure even if its approaches to a number of programming language problems are technically and theoretically sound.

Re:Why I’m not leaving Python for Go by pfraze on Monday September 24, 2012 @11:13AM (#4565046)
That's true, and there's no shame in not knowing a language yet, but I'd still recommend you check it out. It's almost like relearning CS.
Re:Why I’m not leaving Python for Go by _ak on Monday September 24, 2012 @03:16PM (#4566397)
I tried, and failed. Multiple times. Systems that are complicated are not really appealing.
Re:Why I’m not leaving Python for Go by Evbn on Monday September 24, 2012 @11:26PM (#4568407)
And Java programs with Guide and AOP and magical JUnit are hard to understand. You can either not use the weird stuff, or you can learn it.
Re:Why I’m not leaving Python for Go by rogpeppe1 on Monday September 24, 2012 @10:00AM (#4564687)
"So you can actually just write code like this: do val1 <- someValue val2 <- someFunction val1 val3 <- someFunction val2 someFunction val3 then, if any of the values return an error, it gets propagated to the end of the code."

I have seen this (the error monad) mentioned before as a "nice" way of handling errors, even with explicit error returns. I beg to differ - the error messages produced by such a program will be obscure, as all the context is lost - if the first call to someFunction fails, for example, there's not necessarily any indication that the error came from that call rather than the one after it.

Go's explicit error handling means that it's easy to add meaningful context wherever relevant - the error messages printed by such a program are likely to be considerably more useful.

In the end, adding error checks is doing useful work. Each error case should be considered individually, and I've often found it to be the case that it's useful to treat errors as regular values (for example by collecting a bunch of errors, or returning the most important error only).

I can understand the control flow of a Go function by inspecting it on the page (without a glance at the documentation). That's a huge plus.

Re:Why I’m not leaving Python for Go by ufo on Monday September 24, 2012 @11:58AM (#4565249)
In theory the lack of context problem only happens if you use Maybe's Nothing to signal errors. Something else such as Either should allow you to include extra data on the error case.

As for the error collecting bit I think Haskell can do that as well. Errors are just regular values in Haskell and its just a matter of not using do notation or using a diferent error monad if you want to create a list of errors instead of getting out when the first one occurs.

Ill have to agree with you on the control-flow at a glance issue though. This gets really tricky in Haskell, especially when you take lazy evaluation into account.

Re:Why I’m not leaving Python for Go by papsosouid on Tuesday September 25, 2012 @09:28AM (#4570189)
>I beg to differ - the error messages produced by such a program will be obscure, as all the context is lost - if the first call to someFunction fails, for example, there's not necessarily any indication that the error came from that call rather than the one after it.

This is completely incorrect. If you use Maybe, then you get Just value or Nothing, in which case there is no indication of what error occurred. This is used when you don't want to consider what error occurred, simply that the computation failed. If you use Either, then you get an error or a value. The error obviously gives the context you are looking for.

Re:Why I’m not leaving Python for Go by djhworld on Monday September 24, 2012 @03:37AM (#4563614)
I love Haskell too, but I always run into problems with distributing my compiled haskell binaries to other systems that don't have a GHC compiler available to them.

For example, if I compile a binary on my Ubuntu machine and SCP it to a server running centOS Linux, the binary just fails to run because of shared lib issues.

Even if you try and compile the binary statically I still run into similar problems.

This is in stark contrast to any Go/D/C/C++ binary which works fine

Re:Why I’m not leaving Python for Go by pestaa on Monday September 24, 2012 @04:13AM (#4563697)
That'd kind of defeat the whole point of linking a binary. Were you using cabal to build executables? What shared libs were missing on remote machines?
Re:Why I’m not leaving Python for Go by djhworld on Monday September 24, 2012 @07:20AM (#4564065)
When I just do a simple hello world app, I get this error

  ghc helloworld.hs -o helloworld
  <scp binary and ssh to remote machine>
  ./helloworld
  ./helloworld: error while loading shared libraries: libgmp.so.10: cannot open shared object file: No such file or directory
However if I compile statically, I get a different error

  ghc helloworld.hs --make -optc-static -optl-static -optl-pthread -static -o helloworld
  <scp binary and ssh to remote machine>
  ./helloworld
  FATAL: kernel too old 
  Segmentation fault
This guy experiences similar issues.

http://gaiustech.wordpress.com/2010/09/03/on-deployment/

Re:Why I’m not leaving Python for Go by makomk on Monday September 24, 2012 @08:11AM (#4564201)
Amusingly enough, I'm pretty sure "FATAL: kernel too old" is a problem with glibc, so it's actually the C standard library that's preventing you from running your staticly-linked Haskell binary on your server.
Re:Why I’m not leaving Python for Go by djhworld on Monday September 24, 2012 @08:35AM (#4564292)
So the haskell RTS requires the same version of glibc on both machines to make it work?

This wouldn't be too much of a problem but I don't have administrative privileges to these servers. So when I want to write throwaway scripts or programs, I find myself turning to Go or D and they work without any quibbles.

Re:Why I’m not leaving Python for Go by makomk on Monday September 24, 2012 @09:27AM (#4564516)
Nope. In theory you'd have exactly the same problem with any statically-linked binaries compiled on that machine and run on that server, regardless of what language they're written in. glibc has a minimum kernel version requirement and the glibc you're statically linking against just plain isn't compatible with the kernel on the machine you're running it on. Dynamically linking to a newer glibc and running against an older one doesn't generally work either, and IIRC may even result in apps that appear to start but crash unexpectedly when they try to access versions of library calls that aren't there, again regardless of language used.

Your problems with the non-statically-linked version, on the other hand, can probably be solved by copying the appropriate libraries to a directory on the server and pointing LD_LIBRARY_PATH at it... at least until you run into glibc problems.

Basically, what you're doing isn't supported and the fact that it worked for you with languages other than Haskell is mostly luck.

Re:Why I’m not leaving Python for Go by mdwrigh2 on Monday September 24, 2012 @12:55PM (#4565640)
I don't believe Go's GC suite of compilers use glibc at all, for the record.
Re:Why I’m not leaving Python for Go by Evbn on Monday September 24, 2012 @11:24PM (#4568398)
How old is your kernel, anyway?
Re:Why I’m not leaving Python for Go by djhworld on Thursday September 27, 2012 @03:47PM (#4582509)
Running 2.6 I believe, but these servers are not managed by me, they're owned and administered by my workplace, so I don't have any say on upgrades etc