Hacker News new | past | comments | ask | show | jobs | submit login

That code doesn't compile because fromString returns Maybe StringEndingInDot and toString takes StringEndingInDot. So it does protect you from misuse.



A properly enforced static type would not need to emit Maybe, because it would be impossible to set it to the wrong value in the first place.

Not to mention that to be truly useful a static type would need some sort of literal that would emit a compile-time error if the supplied init data was wrong.

In short, examples above do not demonstrate Haskell's ability to statically enforce rules like "string needs to end with a dot".

Now, you could make a type that always prepends a dot when asked for a string representation, which happens to enforce this specific constraint, but you cannot use a trick like this for most constraints (such as the second example of "only alpha characters").


The problem, as you hint at, is that Haskell lacks a literal syntax for "strings that end with a dot". One could fake this up with Template Haskell if one was so inclined. On the other hand, the representations proposed are representations of "strings that end with a dot" and "alphanumeric strings", the dubious ergonomics (and utility)


I think (but cannot guarantee) there's nothing much stopping you writing template Haskell to construct valid values at compile time if you want. It's just that most of the time you're (or at least I am) happy to use "unsafe" functions because the verification is simple enough to do in your head, and the other 99% of the time I'm creating values it's from run-time data.




Consider applying for YC's Fall 2025 batch! Applications are open till Aug 4

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: