Re: Structured exception information



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/
.



Relevant Pages

  • Re: VB.NET-101 Constructors and Methods
    ... All reference type objects require a constructor in order to be ... and if you supply a string, you set up the initial content. ... Private mAlias As String ... Private Sub New ...
    (microsoft.public.dotnet.languages.vb)
  • Re: copy contructor
    ... > I'm writing some code in object oriented Perl, ... about overloading the = operator as a copy constructor. ... > the variables of a class / package. ... accessed as a hash variable named by the package name followed by two ...
    (comp.lang.perl.misc)
  • Re: enforce override of method
    ... public void foo() ... abstract class B_Base extends A { ... same package) a protected or public one. ... And of course anyone who mentions that A's constructor could have code ...
    (comp.lang.java.programmer)
  • Re: passing a Factory to a method to create a generic instance
    ... IOStream ios = new FileIOStream; ... I'd rather do it all properly in the constructor myself, so that we have a guarantee that in all cases, instances are properly initialised. ... Read a token from the stream, associate that token with a class, instantiate the class with newInstance. ... JUnit makes unit test objects that are part of the same package as the classes they test. ...
    (comp.lang.java.programmer)
  • Re: Why constructing functions is a mess [was Language lawyer question: task activation
    ... (as is explained in a 1994 thread Constructor in ADA9X) ... It is not a compiler bug. ... package P1 is ... type T1is abstract tagged limited private; ...
    (comp.lang.ada)