On Wed, Aug 28, 2013 at 11:19 AM, David Piepgrass <[email protected]>wrote:

>
> From: Bill Myers <[email protected]>
>>
>> I was reading a proposal about adding "datasort refinements" to make enum
>> variants first-class types, and it seems to me there is a simpler and more
>> effective way of solving the problem.
>>
>> The idea is that if A, B and C are types, then "A | B | C" is a
>> "structural" enum type that can be either A, B or C.
>>
>> ...
>
>
> Hi, I'm just your friendly everyday lurker... Assuming there's nothing in
> this proposal that fundamentally breaks the language, I just wanted to say
> I think this idea is fantastic. You almost don't need Some(T) since
> Option<T> could just be None|T.
>
> I would add that I recall recently some people were discussing how to do a
> "variant" type in Rust. If all struct types in a program are assigned a
> unique tag, this very same feature could support a variant type, which is
> defined as the union of all types. Then any struct converts implicitly to
> variant (oh, and since I just wrote a parser generator that supports
> inverted sets like ~('a'..'z'), it occurs to me that the type system could
> readily support "any type except A | B" as well.)
>
> The tag could either be a unique integer index, or a pointer to a global
> variable that contains type information (the latter, it seems to me,
> automatically solves the dynamic linking problem "how do we ensure two
> unrelated DLLs have unique tags?" DLL loaders already support relocation in
> case of conflicts.)
>
> Oops, I just remembered that Rust implements enum types using a fixed-size
memory area that is as large as the largest possible item. So a variant (or
"anything except") type, having unlimited size, doesn't directly fit into
that model.

But the compiler could switch to a heap representation for variants. Or,
better, variants could use a smart fixed-size representation in which heap
allocation is used only when the variant is storing a larger type. For
instance, a variant could always use two words, one word for the type tag
and one word for the value. If the current data type T is one word or
smaller, it is stored directly in the second word. Otherwise, the second
word is a ~T pointer.

Just an idea.
_______________________________________________
Rust-dev mailing list
[email protected]
https://mail.mozilla.org/listinfo/rust-dev

Reply via email to