3 ms·
I believe the way this is solved is by having the type of escaped(foo) different than that of parse(foo) and only accepting a EscapedString in doBaz and an Pars
by Misdicorl 8y ago
I believe the way this is solved is by having the type of escaped(foo) different than that of parse(foo) and only accepting a EscapedString in doBaz and an ParsedString in doBar.
Your type structure at no extra runtime cost is
String : ParsedString : EscapedString.
This ensures you don't escape strings before parsing them too. Nice!
- afraca 8y agoThis is common in Haskell as well using `newtype`. You can have type aliases like type A = B but you can use the following function with a B: f :: A -> _ whereas newtype EscapedString = EscapedString String f' :: EscapedString -> _ would prevent from using f' with the wrong data. Newtype is a zero-cost abstraction.