From: Shark8 <onewingedshark@gmail.com>
Subject: Re: Type invariants and private extensions?
Date: Thu, 14 Sep 2017 00:28:29 -0700 (PDT)
Date: 2017-09-14T00:28:29-07:00 [thread overview]
Message-ID: <45fe84b9-3fea-44e9-ab87-c91370a0cd2e@googlegroups.com> (raw)
In-Reply-To: <op9os0$1mg$1@gioia.aioe.org>
On Tuesday, September 12, 2017 at 4:59:17 PM UTC-6, Victor Porton wrote:
>
> Thanks.
>
> But this seems not to solve my problem:
>
> The base type for Example cannot be defined as a discriminated type for
> certain Type_Enumeration, because it is possible that when creating the
> object it may be yet unknown what the value of the discriminant should have
> (my main problem is to invent somethings if this value is known at object
> creation, but we must support the unknown case too). AFAIK, it is not
> possible to change the discriminant later.
What are you doing that you need to create an object w/o knowing the types? I've used the variant-record approach in several interpreters to excellent effect; I've also used tagged-type hierarchies, too.
Perhaps what would be more called for in your design is an abstract base type with a discriminant, then having the child-types use the proper value and using Base'Class variables/returns as needed:
Type Base( Item_Type : Type_Enumeration ) is abstract tagged null record;
Type Integer is new Base( TInteger ) with null record;
Type Boolean is new Base( TBoolean ) with null record;
--...
>
> The solution would be to define a discriminated type as its descendant, but
> then all problems which I described in previous messages appear again. So
> this is not a solution.
>
I think you're tripping yourself up by overcomplicating / underelaborating on the design. Clear your mind, pull put a piece of paper, and in pen [so you can't erase] write a description of the system being careful to keep it consistent -- I find it very helpful nailing down a design. (In my experience how you're presenting things is how I feel when I haven't solidified a good design yet, and so things keep swirling out of control... kind of the infinite-loop version of "analysis paralysis".)
> Hm, we can make Type_Enumeration to contain the value "Any" to allow any
> result type. This is formally a solution, but it is against OO principles
> such as the principle that derived types should be made by object
> inheritance. Wait, it has another more important in practice deficiency:
> Example(Any) would be incompatible with Example(TInteger); this seems not
> acceptable.
Again, see the above.
> And variant records are certainly not applicable here, as I deal with
> (tagged) private types which contain a C pointer only.
That actually might be all the more reason to use them: C-pointers are notorious for being unsafe WRT type-safety... and a variant-record could certainly be used to isolate them and ensure that they are only used in the places they ought.
next prev parent reply other threads:[~2017-09-14 7:28 UTC|newest]
Thread overview: 28+ messages / expand[flat|nested] mbox.gz Atom feed top
2017-09-11 19:51 Type invariants and private extensions? Victor Porton
2017-09-11 20:00 ` Egil H H
2017-09-11 20:48 ` Victor Porton
2017-09-11 21:19 ` Egil H H
2017-09-11 21:27 ` Victor Porton
2017-09-11 21:49 ` Egil H H
2017-09-11 22:00 ` Victor Porton
2017-09-11 22:06 ` Egil H H
2017-09-12 7:30 ` Dmitry A. Kazakov
2017-09-11 22:00 ` Jere
2017-09-11 22:02 ` Victor Porton
2017-09-12 18:26 ` Jeffrey R. Carter
2017-09-12 18:54 ` Victor Porton
2017-09-12 19:56 ` Jeffrey R. Carter
2017-09-12 20:08 ` Victor Porton
2017-09-12 22:34 ` Shark8
2017-09-12 22:59 ` Victor Porton
2017-09-13 4:21 ` Jere
2017-09-13 4:28 ` Jere
2017-09-13 4:34 ` Jere
2017-09-14 13:52 ` Victor Porton
2017-09-15 0:48 ` Jere
2017-09-16 14:22 ` Victor Porton
2017-09-14 7:28 ` Shark8 [this message]
2017-09-14 13:56 ` Victor Porton
2017-09-14 13:58 ` Victor Porton
2017-09-11 22:48 ` Shark8
2017-10-02 23:16 ` Randy Brukardt
replies disabled
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox