Re:What I'd like to see in Go 2.0
by amw-zero on Friday February 04, 2022 @09:36AM
(#30207221)
This compiles:
So this isn't a good suggestion, because you can easily pass any int value and will not get a compiler error. You may as well be using strings at that point.
[ Parent
| on HN ]
I generally tend to use enumer[0] to generate some boilerplate code that can help with addressing this, e.g. the below would compile, but would error at runtime. There are probably linters out there that could catch this. With Go, linters are generally pretty good at catching this kind of stuff.
package main
import "fmt"
type Test int
const (
T1 Test = 0
T2 = 1
)
func main() {
t, err := TestString("T1")
if err != nil {
panic(err)
}
TestSomething(t)
}
func TestSomething(t Test) {
fmt.Println(t.String())
}
Having said that, it seems weird to have to mimic enums, as opposed to actually having it. Doesn't feel like it would add much complexity, if at all.> as opposed to actually having it
Like C or C++ do? :)
Right. However, C and C++ are far more complex, with no memory safety. Everything has it's ups and downs.
Re:What I'd like to see in Go 2.0
by schrodinger on Friday February 04, 2022 @10:04AM
(#30207605)
Your point is valid, but the Go philosophy depends on you following conventions to have reliable code. This is true all over the place, e.g. you can easily ignore errors.
Other languages take a stricter approach, and maybe that's better. Not defending (although I like Go), but it's really more a language philosophy than a singular defect.
As the other commenter noted, this should fail code review and you should be using the provided constants, and it should be clear to you. And if you disagree (which again is totally valid), you should use a stricter language—there's plenty out there!
Fwiw a literal 17 in a function call, let alone anywhere outside an equation or constant definition is a code smell that should never make it past review.
I see your point however.
Depending on code review instead of a static type system does not scale. Look at all of the memory safety security vulnerabilities that are solved by "simply making sure to manage memory correctly."
Also:
variableDefinedInAFarAwayModule := 17
...
TestSomething(variableDefinedInAFarAwayModule)
It's not always as clear as a constant value being passed to an incorrect type.I don't understand your example here, that's not going to compile.
variableDefinedInAFarAwayModule is definitionally type int and will not be cast. It is also unpublished, so you couldn't be using it for a faraway module?
Your 17 in the previous example has it's typed determined at compile time which is why it can be a problem.
Ah thank you. That's slightly better then.
This will not compile because the type of the variable is int not Test.
I mean, that's a pretty common usage of enums, isn't it?
TestSomething(T1 & T2)