Re:OCaml for the Masses (2011)
by lolive on Monday November 26, 2018 @07:30PM
(#18538182)
I have heard about Rust. But I wonder if you can do something that a OO programmer (like me :) would call "string subclasses" so you do not manipulate a generic type called string, but a meaningful subclass such as "FirstName" or "LastName", or "TownName".
[ Parent
| on HN ]
Re:OCaml for the Masses (2011)
by steveklabnik on Monday November 26, 2018 @07:41PM
(#18538248)
You wouldn’t do it as a “subclass”, as Rust doesn’t have classes or inheritance.
You’d use the “new type pattern”, which is like a named 1-tuple:
struct FirstName(String);is this type interchangeable with String?
I'm not the person you asked, but I don't think domain-specific user-defined types (whether defined as subclasses or subranges (see Pascal) of existing language types, or another method as Steve said), are supposed to be automatically interchangeable or equivalent to the existing type they are derived from (without a cast or conversion of some kind), otherwise it would defeat the purpose, which is better type or range checking.
Re:OCaml for the Masses (2011)
by steveklabnik on Tuesday November 27, 2018 @09:35AM
(#18542555)
Not exactly. That is, you can't pass it directly to a function that takes a String, but if we made the inner string public, we could extract it with two characters. If we don't want to make it public, we can write an accessor as well, etc.
As far as i understand, Rust is strongly typed, has generics and no type erasure. I also suppose String is some kind of a primitive type. Could it be possible to make it parametrizable, so we can manipulate a String<FirstName> as a String, but the compiler will never confound it with (for example) String<LastName>.
Re:OCaml for the Masses (2011)
by steveklabnik on Tuesday November 27, 2018 @11:55AM
(#18543995)
String is a library type, not a primitive type.
You could write your own string parameterized in this way, but it would be incompatible with the standard library String and so you’d need the same conversion steps.
What would happen if the Rust core team decided to parametrize String? Would it work with legacy code? (that creates something like String<?> and consume String<?>).
Re:OCaml for the Masses (2011)
by steveklabnik on Monday December 03, 2018 @08:15AM
(#18588495)
It cannot be done in a backwards compatible way with today’s language features.