Hacker Newsnew | past | comments | ask | show | jobs | submit | pxia's commentslogin

When the gain is smaller than the cost of hiring people to do the claim.


Because you want to represent errors. For example a find() function that returns the index of some element: you want to return -1 when the element doesn't exist.


Shouldn't idiomatic go return an error instead? Magic number ranges for errors always seemed a bit old-fashioned.


It's just an example... Another example would be you want to iterate your array in reverse. The normal `for (uint i=size-1; i >= 0; i--)` won't work.


That example doesn't work precisely because you're conflating the concept of the loop counter (which type represents an integer that needs to be able to go <0) with the array index accessor. There's no reason for these two things to be the same type, it's just a pattern that accidentally works because we allow arrays to be accessed using a type that represents negative numbers.

What would be a more compelling argument is to use int in the same way that python does - a negative accessor means offset from the end of the array, not the start. I'm not sure golang lets you do this though...?


Returning -1 instead of an error is abusing the type system. In C, there is no error type, so it might be excusable there, but it is inexcusable in Go, IMO.


Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: