Re: Structured exception information
- From: Maciej Sobczak <no.spam@xxxxxxxxxxx>
- Date: Tue, 16 Jan 2007 10:04:48 +0100
claude.simon@xxxxxxxxxxxxxxxxxx wrote:
when My_Type is an indefinite subtype.
type My_type (<>) is ... for example
Then, when you declare an object of that type you must provide an
initialization expression (a constructor call !).
If I understand correctly:
procedure Hello is
package P is
type T(<>) is private;
function Constructor(S : String) return T;
private
type T(I : Integer) is
record
null;
end record;
end P;
package body P is
function Constructor(S : String) return T is
begin
return (I => 0);
end Constructor;
end P;
X : P.T := P.Constructor("some params");
begin
null;
end Hello;
(of course, package is nested for presentation only)
I have initially thought that discriminated types limit me in terms of what parameter types can be used for constructing them, but actually there is no such limitation - constructor function can have whatever parameter types it wants and the discriminant is irrelevant.
It does not feel like a genuine part of the object model (you know, in C++ it is much simpler, more readable, maintainable, etc. ;-) ), but seems to work.
OK, so putting aside the fact that my compiler can not swallow it, is it possible to make T Limited_Controlled as well?
This is my guess:
-- file p.ads
with Ada.Finalization;
package P is
type T(<>) is limited private;
function Constructor(S : String) return T;
private
type T(I : Integer) is new Ada.Finalization.Limited_Controlled with
record
null;
end record;
end P;
-- file p.adb
package body P is
function Constructor(S : String) return T is
begin
return (I => 0); -- NOTE
end Constructor;
end P;
-- file hello.adb
with P;
procedure Hello is
X : P.T := P.Constructor("some params");
begin
null;
end Hello;
It should be impossible to create X in any other way - this is the exact effect that I want to achieve.
My compiler does not like it (but it's not really Ada2005), so I cannot verify it. Is the code above correct?
BTW - in the line marked as NOTE the compiler says that aggregate type cannot be limited. Is it true for Ada2005?
--
Maciej Sobczak : http://www.msobczak.com/
Programming : http://www.msobczak.com/prog/
.
- Follow-Ups:
- Re: Structured exception information
- From: Randy Brukardt
- Re: Structured exception information
- References:
- Structured exception information
- From: Maciej Sobczak
- Re: Structured exception information
- From: claude.simon@xxxxxxxxxxxxxxxxxx
- Structured exception information
- Prev by Date: Re: Cannot initialize entities of limited type?
- Next by Date: Re: Structured exception information
- Previous by thread: Re: Structured exception information
- Next by thread: Re: Structured exception information
- Index(es):
Relevant Pages
|