3 ms·
Yes, that's possible in Haskell. And it's actually one of the canonical examples. And even in C you could wrap your (char*)s up in different structs. The typ
by eru 15y ago
Yes, that's possible in Haskell. And it's actually one of the canonical examples.
And even in C you could wrap your (char*)s up in different structs. The typing overhead would be higher than in Haskell, though.
(There's even a language extension to allow you to overload string literals. But you probably don't want that, because
s = TaintedString "My string literal"
is easier to read than:
s :: TaintedString
s = "My string literal"
The overloading makes more sense for implementation details like using a different String implementation like ByteStrings instead of a linked list of Characters. You actually want to be able to see the semantic differences.)
- masklinn 15y agoRe. second one, wouldn't it also be possible to write s = "My string literal" :: TaintedString ? It's not shorter than the constructed version but it should work, you sometimes need it to get the "right" kind of number out of a literal when the type inference gets confused (or more commonly in the REPL)
- eru 15y agoYes, that's possible with the right compiler extensions. For this example it's essentially the same thing as giving the type on its own line, so I chose the more common syntax.