.equals() ?
Consider this java code:
if (o.getApplyType().name().equals(ApplyType.NULL)) ...
If you look carefully at this java code, you’ll see that the condition will never be true. o.getApplyType().name()
is clearly going to be a String
. ApplyType.NULL
is not ever going to be a String
. This condition will never be true.
Java is commonly thought of as “strongly typed”, but this is an example of where it could be more strongly typed. Why even try to check if two things are equal when they are different types?
Go has stronger type checking:
type Meters int
type Feet intfunc main() {
var foo Meters = 42
var goo Feet = 42
if foo == goo {
fmt.Println("they are equal!")
}
}
This program will produce an error:
invalid operation: foo == goo (mismatched types Meters and Feet)
Even though we know, and the compiler knows, that these are int
underneath, the language/compiler is here to help us, and does so by telling us that you cannot check things of different types for equality.
Sometimes, go may seem too strict, but really it’s better that way.