5 ms·
Every type in Go has a zero value. The zero value for pointers is nil. So you can't do it with regular pointers, because users can always create an instance of
by suremarc 3y ago
Every type in Go has a zero value. The zero value for pointers is nil. So you can't do it with regular pointers, because users can always create an instance of the zero value.
- tialaramex 3y agoThis is one of those things which feels like just a small trade off against convenience for the language design, but then in practice it's a big headache you're stuck with in real systems. It's basically mandating Rust's Default trait or the C++ default (no argument) constructor. In some places you can live with a Default but you wish there wasn't one. Default Gender = Male is... not great, but we can live with it, some natural languages work like this, and there are problems but they're not insurmountable. Default Date of Birth is... 1 January 1970 ? 1 January 1900? 0AD ? Also not a good idea but if you insist. But in other places there just is no sane Default. So you're forced to create a dummy state, recapitulating the NULL problem but for a brand new type. Default file descriptor? No. OK, here's a "file descriptor" that's in a permanent error state, is that OK? All of my code will need to special case this, what a disaster.
- tptacek 3y agoThis is a thread about Go, not about Rust. There is a bunch of interesting computer science in this post, and if interesting new computer science is a baby seal, Rust vs. Go discussions are hungry orcas.
- maccard 3y agoI write a decent amount of go - this isn't a defence of the current situation. > All of my code will need to special case this, what a disaster. No, your code should handle the error state first and treat the value as invalid up until that point, e.g. foo, err := getVal() if err != nil { return } // foo can only be used now It's infuriating that there's no compiler support to make this easier, but c'est la vie.
- leftyspook 3y agoMan, if only over 30 odd years of PL research leading up to Go, somebody came up with a way to do it better.
- pstuart 3y agoAnd yet it's a productive language with significant adoption, go figure. They made trade-offs and are conservative about refining the language; that cuts both ways but works well for a lot of people. The Go team does seem to care about improving it and for many that use it, it keeps getting better. Perhaps it doesn't happen at the pace people want but they always have other options.
- stouset 3y ago> And yet it's a productive language with significant adoption, go figure. Perl was also a successful language with significant adoption. At least back then, we didn’t know any better. In twenty years the industry will look back on golang as an avoidable mistake that hampered software development from maturing into an actual engineering discipline, for the false economy of making novice programmers quickly productive. I’m willing to put money on that belief, given sufficiently agreed upon definitions.
- pstuart 3y agoPerl had less competition and also suffered from being more of a "write-only" language. Lisp, Haskell, OCaml all likely tickle your PL purity needs, but they remain niche languages in the grand scheme of things. Does that make them bad? I think Go will be the new Java (hopefully without the boilerplate/bloat). It's good enough to do the job in a lot of cases and plenty of problems will be solved with it in a satisfactory manner. Language wars are only fun to engage with for sport, but it's silly to get upset about them. Most languages have value in different contexts and I believe the real value in this dialog is recognizing when and where a language works and to accept one's preferred choice may not always be "the one".
- 3y ago
- andreyvit 3y agoDefault gender male not how this works in practice. Instead, you define an extra “invalid” value for almost every scalar type, so invalid would be 0, male 1 and female 2. Effectively this makes (almost) every scalar type nullable. It is surprisingly useful, though, and I definitely appreciate this tradeoff most of the time. (Sometimes your domain type really does have a suitable natural default value, and you just make that the zero value.)
- stouset 3y agoGreat, now you’ve brought the pain of checking for nil to any consumer of this type too!
- codetrotter 3y ago> Default Gender = Male is... not great enum Gender { Unspecified, Male, Female, Other, } impl Default for Gender { default() -> Self { Self::Unspecified } } or: enum Gender { Male, Female, Other, } and use Option<Gender> instead of Gender directly, with Option::None here meaning the same that we would mean by Gender::Unspecified
- unrealhoang 3y agoI think they are talking about the cons of Go allowing zero value. Rust doesn’t have that problem.
- Kamq 3y agotype Gender int const ( Unspecified Gender = iota Male Female Other ) Works the same way. Declaring an empty variable of the type Gender (var x Gender) results in unspecified.
- stouset 3y ago…and now you need to check for nonsense values everywhere, instead of ever being able to know through the type system that you have a meaningful value. It’s nil pointers all over again, but for your non-pointer types too! Default zero values are yet another own goal that ought to have been thrown away at the design stage.
- Kamq 3y ago> instead of ever being able to know through the type system that you have a meaningful value. That's... not what I'm looking for out of my type system. I'm mostly looking for autocomplete and possibly better perf because the compiler has size information. I really hate having to be a type astronaut when I work in scala. So, I mean, valid point. And I do cede that point. But it's kind of like telling me that my car doesn't have a bowling alley.
- jasonhansel 3y agoOne answer would be to provide something like a GetPointer() method which, if the inner pointer is nil, creates a new struct of type T and returns a pointer to it.
- stouset 3y agoTurns out worse isn’t better after all. Who could have guessed?