Live data from Hacker News

Compile-time safety for enumerations in Go

vladimir.varank.in

61–70 of 116 posts

Re: Compile-time safety for enumerations in Go

#61
post #56
post #54

Earlier quoted context omitted.

Exactly. Someone could always connect to your program with a debugger and modify the instructions are being run, and that's why you need to regular runtime checks of the validity of your program text. If you want to handle people actively trying to undermine the assumptions your program is making, you can't leave out easy to replicate cases like 'gdb attach', obvious, non-accidental use of the type system, and other…

That doesn't make any sense to me. Most instructions are not atomic, so even if you had runtime checks something like a debugger could still inject invalid state in between your checks, making them ineffective. If real-time code injection is your threat model, I don't see how runtime checks would get you anywhere.

You seem to be suggesting that (to use a common phrase) if something can be done, it should be done. The previous poster's example regarding a debugger was to challenge the notion that there was any meaningful reason to constrain non-accidental type subversion. You seem to be saying it's absurd to care about someone using a debugger to inject invalid code at runtime because there's nothing you can do about it, thus implying if you can do something, you should. But that only begs the question--why should you? To some people, defending against debugger code injection is no more absurd than defending against other examples where the programmer deliberately alters code to subvert static type checking. If you disagree then articulating more meaningful distinctions among cases would be worthwhile. Mere ability is generally not considered sufficient justification alone to do something.

Re: Compile-time safety for enumerations in Go

#62
You can implement the interface from other packages through embedding

    type ColorWrapper struct {
        color.Color
        ...
    }
ColorWrapper will implement color.Color. I don't know what the point would be, but another way to break the compile-time type safety.

Re: Compile-time safety for enumerations in Go

#64

I would be interested to read bug reports from people who thought they had a complete enumeration but learned in production that their enumeration was incomplete. I've never seen it myself.

At my current day job we have many, many such "enums" that slowly expand over time. I've seen many bugs from new values not being taken into account in the several places one has to remember to double check. Sometimes testing catches these, sometimes it doesn't.

Re: Compile-time safety for enumerations in Go

#65
post #63

Go doesn’t have type-safe enums in the language? That’s wild, even C compilers have this feature now (optionally), and then there’s Rust/Swift’s capabilities.

Welcome to Go. “Wait, Go doesnt have X?” I’m convinced its an incomplete language masquerading as a simple one.

Re: Compile-time safety for enumerations in Go

#67
post #63

Go doesn’t have type-safe enums in the language? That’s wild, even C compilers have this feature now (optionally), and then there’s Rust/Swift’s capabilities.

Welcome to Go. “Wait, Go doesnt have X?” I’m convinced its an incomplete language masquerading as a simple one.

Well, that's an opinion. Depending on how you define "complete," any language with fewer features than Scala might be considered incomplete

Unless you mean: is it enough of a language to be useful for its intended usages? In which case my decade of paychecks would like to let you know that it is, in fact, sufficiently complete for many business needs.

Re: Compile-time safety for enumerations in Go

#68
post #50

Earlier quoted context omitted.

> If your module relies on compile time checks for validity, ...then it's broken, because the Go compiler, factually, cannot (in general) check or enforce the validity of specific values. > there's no reason for you to also include runtime checks. Other developers breaking your defined contract is not your responsibility, it's not even a bug. It is absolutely the responsibility of the function func PrintColor(c Color…

It’s actually not and is one of those Java isms that have made its way into generalized software engineering. If I write a function that takes Foo as an argument. I have a Foo implementation exposed elsewhere in my program. It is absolutely expected that I mean MY Foo, not your Foo. If you pass me your Foo, you will get unexpected results that are not a bug, not a side effect, not my responsibility. It’s yours, the c…

> it’s is not my responsibility to validate all permutations of unknown types to ensure you’re passing me mine.

Of course it is! If you provide an API that takes a Color, and you define valid Colors as exclusively Red, Green, and Blue, then you are absolutely responsible for validating input Color values and rejecting anything which is not Red, Green, or Blue. If you delegate that responsibility to the caller, then your API provides no meaningful encapsulation, and isn't a useful abstraction -- it's entirely leaky. No bueno.

Re: Compile-time safety for enumerations in Go

#69
post #29

Earlier quoted context omitted.

The article is titled "Compile-time safety for enumerations in Go" but my example demonstrates that this is not the case.

It is the case under certain conditions. Casting is by definition an escape hatch.

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

#70
post #16

Earlier quoted context omitted.

> I still feel as though languages without sum types are missing an important data and state modelling tool. That's because they are. jerf subtly shifts the point to one they can criticise, but it does not change the basic fact that sum types are a critical tool which is just... missing. There's probably no tool which can't be misused, even the humble boolean, that's not an issue with the tool, and pointing that out…

Where I'd disagree is that they are a tool, not a critical tool, and they aren't missing from Go, they are simply not the preferred choice. You can cover 75% of the use cases with this approach. It is not 100% of the use cases. But there isn't a language where you can cover 100% of the use cases with 100% effectiveness, which is why we don't have and never will have The One True Language. Moreover, Go seems to attrac…

> Sum types are great, until you hit the branch of the expression problem where you really need the other side, and if you're in a language that favors them, you're going to get the same 75% experience, just mirror imaged.

Which other side? Product types (aka structs)? I don't think there are any languages with Sum types that don't also include Product types.

Post reply on HN