Re:Compile-time safety for enumerations in Go
by revoly on Friday September 29, 2023 @11:16AM
(#37705494)
I don't think the solution is trying to cover this. If a user does type conversion then usually they know what they are doing. It's like going out of the way. The solution is to just hide a type behind an interface to be explicit about it and to avoid the error that would have gone unnoticed otherwise.
[ Parent
| on HN ]
Re:Compile-time safety for enumerations in Go
by throwaway894345 on Friday September 29, 2023 @11:22AM
(#37705588)
The type conversion is a red herring. He's just changing the value of a variable by referencing and dereferencing a pointer. In other words, he's not using a pointer conversion to change the value of `color.Red`, he's creating a new variable `c` and assigning `color.Red` to it, and then changing the value of the variable through the pointer and type conversion. But he doesn't even need to do the pointer/type conversion stuff, he could just do `c = Color("orange")` and call it a day.
Re:Compile-time safety for enumerations in Go
by kiitos on Friday September 29, 2023 @12:09PM
(#37706279)
> But he doesn't even need to do the pointer/type conversion stuff, he could just do `c = Color("orange")` and call it a day.
c = Color("orange") does not compile.
Re:Compile-time safety for enumerations in Go
by throwaway894345 on Friday September 29, 2023 @03:29PM
(#37709046)
Ah, I misunderstood what type color.Red had. My mistake.
Re:Compile-time safety for enumerations in Go
by kiitos on Friday September 29, 2023 @12:14PM
(#37706349)
The article is titled "Compile-time safety for enumerations in Go" but my example demonstrates that this is not the case.
Re:Compile-time safety for enumerations in Go
by taeric on Friday September 29, 2023 @12:21PM
(#37706444)
The counter point is that this is like arguing that an article about safe knife skills are silly, because you can still just stab yourself?
Re:Compile-time safety for enumerations in Go
by kiitos on Friday September 29, 2023 @12:56PM
(#37706960)
I'm not sure how that's comparable. Compile-time type safety is a boolean thing, you either have it or you don't. If you have it, then my example shouldn't compile. But it does compile, so you don't have it.
Re:Compile-time safety for enumerations in Go
by taeric on Friday September 29, 2023 @01:55PM
(#37707867)
I think by insisting to view it only as a boolean thing is very limiting. By that measure, are the type safety hints of Python not really giving any type safety? Just give up and go home, as it were? :D
I'm sympathetic to the points, I think. But I also don't have a problem with someone pushing for some type safety as long as you avoid actively trying to go against it.
Re:Compile-time safety for enumerations in Go
by kiitos on Friday September 29, 2023 @02:43PM
(#37708510)
> By that measure, are the type safety hints of Python not really giving any type safety?
If we're talking compile-time, then: yes, absolutely!
Type hinting doesn't provide type safety, because type safety isn't a spectrum.
Re:Compile-time safety for enumerations in Go
by throwaway46281 on Saturday September 30, 2023 @09:18AM
(#37715157)
To me it's pretty clear that safety is a spectrum (e.g. we give cars safety ratings), and therefore so is type safety.
Is "type safety" something you saw defined in a PL book or something, which has a more rigid definition to you?
Re:Compile-time safety for enumerations in Go
by arp242 on Friday September 29, 2023 @09:54PM
(#37712166)
Out of 10 possible errors a compiler can catch 0, 1, 2, 3, etc. up to 10.
A compiler that catches 7 errors is more safe than one that catches 4, and one that catches 4 is more safe than one that catches 0.
Re:Compile-time safety for enumerations in Go
by dgb23 on Friday September 29, 2023 @05:40PM
(#37710417)
You demonstrated that you can deliberately circumvent it.
Re:Compile-time safety for enumerations in Go
by paulddraper on Friday September 29, 2023 @06:23PM
(#37710808)
It is the case under certain conditions.
Casting is by definition an escape hatch.
Re:Compile-time safety for enumerations in Go
by kiitos on Friday September 29, 2023 @08:12PM
(#37711633)
Casting (in the way I did it in my example) is entirely normal and common and in no way an escape hatch.
Re:Compile-time safety for enumerations in Go
by paulddraper on Friday September 29, 2023 @10:13PM
(#37712242)
Ask yourself: what does casting do?