From: | George Neuner <gneuner2@comcast.net> |
Newsgroups: | comp.compilers |
Date: | Thu, 06 Jan 2011 15:45:23 -0500 |
Organization: | A noiseless patient Spider |
References: | 10-12-040 10-12-052 10-12-064 11-01-008 |
Keywords: | types, design |
Posted-Date: | 06 Jan 2011 15:52:18 EST |
On Fri, 31 Dec 2010 17:02:23 -0500, noitalmost <noitalmost@cox.net>
wrote:
>For now, I'm not going to include variant records in my language, but I'm
>going to try to keep my options open for later. So I guess I'd like an example
>of where it would be nice to have a variant record. So, assuming our language
>is C++ (so we eliminate cases in C where a union is used only because we don't
>have inheritance), can someone give me a system progamming example that
>requires a union? I'm still looking for that "aha!" moment of its usefulness
>if the language has classes.
The prototypical system programming case is network packet
translation: a packet having an unknown format is read into a buffer
and then various "templates" are overlaid on the buffer to decode the
data.
And yes, this can be done without using variant records, but the
coding is somewhat simpler using them. Because network packet data
contains identifying tags, when using variant records the programmer
doesn't have to manually locate and examine the tags and select a
static record format to overlay.
On the application side, variant records can be useful for FFI
(foreign language functions) and for serialization. Generally, any
kind of tagged data transfer may be fodder for a variant record. It
doesn't matter whether the data is packetized or streamed - in the
stream case you just need to make sure you've got a whole record
before you try to do something with the data.
>Also, is alternation the general term for this type of construct, or is that
>just the way one says union in ML?
"Alternation" is a type theoretic term referring to the set of members
that make up a particular variant case. I use the term "alternation"
preferentially because, depending on the language under discussion,
the terms "variant" and "case" may be overloaded and, without
qualification everywhere, the discussion can quickly become confusing.
ML calls its variant records "algebraic" or "sum" types. In ML the
variant tags are hidden from the programmer and managed by the
compiler. The correct alternation of a sum type is selected by
pattern matching against the (maybe partial) list of field names to be
accessed.
George
Return to the
comp.compilers page.
Search the
comp.compilers archives again.