From: | Robert A Duff <bobduff@shell01.TheWorld.com> |
Newsgroups: | comp.compilers |
Date: | Fri, 24 Dec 2010 12:50:20 -0500 |
Organization: | The World Public Access UNIX, Brookline, MA |
References: | 10-12-040 |
Keywords: | design |
Posted-Date: | 25 Dec 2010 16:26:44 EST |
noitalmost <noitalmost@cox.net> writes:
> My question is this: Would variant records provide significant value to a user
> of my language (given that classes were implemented)?
No.
Take a look at Ada as an example of a language that has both features.
It's for historical reasons: Ada 83 had variant records (pretty much
the same as Pascal), and object-oriented features were added in the
1995 version.
A variant record is basically an upside-down class hierarchy.
Ada's variant records can do some things that classes can't. Variant
records are not extensible, so the compiler knows all the variants,
and so does the programmer. If there are four variants, the tag field
can fit in 2 bits. You can't do that with classes (unless you're
willing to do whole-program analysis). And you can do case statements
on the tag field of a variant record, and the compiler can check that
you didn't forget any of the possibilities.
You could decide you don't care about these "features" of variant
records. Or you could design your class feature so that the
programmer can optionally specify all the possibilities up front.
Usually, you don't want to do that, because you want extensibility.
But there are exceptions. For example, in Smalltalk, Boolean is a
class with two subclasses True and False. But the extensibility there
is an illusion: if somebody added a third subclass of Boolean, it
would break most programs. Boolean is fundamentally two-valued; it's
not at all "object oriented".
You should also check out how types are declared in OCaml.
- Bob
Return to the
comp.compilers page.
Search the
comp.compilers archives again.