|
![](/i/fill.gif) |
Warp wrote:
> Darren New <dne### [at] san rr com> wrote:
>> The destructor semantics is one of the things I think C++ really did get
>> just right.
>
> The proper name for that mechanism would be "RAII" (which might be
> somewhat of a misnomer, as it doesn't fully express what it means).
I think the two are separate, but "RAII" is the best (maybe even "intended")
way to use the C++ destructor semantics. Certainly the semantics don't
change if you screw up your RAII. :-)
> (In principle RAII is not incompatible with garbage collection, so
> conceivably you could have both in the same language.)
Indeed. Especially if you use something like reference counting and deal
with circular references specially or something. Unfortunately, the fastest
garbage collectors are the ones that never touch the garbage, so it's hard
to do this well without having basically a separate heap for objects with
non-memory destructors, which I think is what we'll eventually start seeing
in some of these run-time implementations, if we don't start seeing GC-smart
operating systems first. (And I'd even count Erlang in that latter
categorization, since all interaction outside the Erlang semantics goes thru
a "port" kind of construct rather than a function call kind of construct.)
> According to wikipedia, C++ is not the only language using RAII, and
> mentions Ada as another one. I didn't know that.
I think that's ... stretching it a bit. Sounds like Ada folks trying to
convince C++ folks they should switch or something. :-)
Ada is very non-orthogonal in its data structures (even more so than C++).
Objects are basically declared as "records with a vtable" or so, and if your
data type isn't a type of record (in the Pascal/Algol sense of the word, or
what C would call a struct), then you don't get to make it an object.
Ah. They're called "controlled types."
http://www.adaic.org/docs/95style/html/sec_9/9-2-3.html
Basically, an object type that inherits from Ada.Finalization.Controlled.
Inheriting from that type gives you three methods: Initialize, Finalize and
Adjust. Finalize is like the destructor, and Adjust is sort of like a
copy/assignment constructor, only more limited. (When you assign to a
controlled type, it copies all the hardware bits, then invokes Adjust on the
copy. You don't get to change what you assigned *from*, because that would
be confusing. Use a procedure with two in/out arguments for that. :-)
However, this only applies to objects derived from Controlled. It doesn't
work with strings, arrays, any of the built-in collections, files, tasks,
loadable packages, pointers, etc etc etc. Ada is really pretty
non-orthogonal in that sense. You can't mix and match. (Yes, that sucks. :-)
I.e., much like you can't have constructors and destructors on pointers or
integers in C++, except even for some very complicated types in Ada.
Note that any type can be declared "limited" too, which basically means the
assignment operator is private. A record that can do inheritance (i.e., that
has a vtable) is called a "tagged type". In case you get interested and read
some of the following pages. :-) Note that "class type X" means "X and all
its descendants" as opposed to "type X" which means just type X. Declaring a
procedure with an argument that is a class type is how you get run-time
dynamic inheritance-based dispatch as opposed to overloading. The "with
private" declaration is like declaring something "struct xyz;" in C or C++.
A "protected" type is basically a monitor in the multitasking definition of
the word.
Blah'goop is a way of getting the goop property or type out of the blah
variable or type. So myarray'length, or mytaggedtype'parent, or
localvariable'address (&localvariable) or something like that.
Ada has some very unusual yet precise terminology. It's fun to read a
sentence talking about a controlled atomic volatile protected limited class
type and have an idea of what that means. ;-)
--
Darren New, San Diego CA, USA (PST)
Serving Suggestion:
"Don't serve this any more. It's awful."
Post a reply to this message
|
![](/i/fill.gif) |