From: "Yannick Duchêne (Hibou57)" <yannick_duchene@yahoo.fr>
Subject: Conditional holder for a type with unknown discriminant
Date: Sat, 27 Oct 2012 04:50:52 +0200
Date: 2012-10-27T04:50:52+02:00 [thread overview]
Message-ID: <op.wmtgu2dfule2fv@cardamome> (raw)
Hi Ada hackers (lol, does this word can comes Ada?),
Not the first time I face this question, just that it came back to me.
The matter: a conditional holder à‑la SML is an idiom easily expressed in
Ada, except when the object to be wrapped also has to have an unknown
discriminant.
Example:
type E_Type is private;
type W_Type
(Is_Null : Boolean)
is record
case Is_Null is
when True => null;
when False => E : E_Type;
end case;
end record;
function F return W_Type;
`W_Type` is the result of a request returning an element of type `E_Type`,
which may return nothing.
Pros: avoid testing of condition dissociated from the result.
Cons: troubles when `E_Type` has unknown discriminant.
type E_Type (<>) is private;
type W_Type
(Is_Null : Boolean)
is record
case Is_Null is
when True => null;
when False => E : E_Type;
end case;
end record;
function F return W_Type;
Oops, unconstrained subtype in record definition.
You may give default values to discriminant, but no there may be no well
good enough defaults, and you may also want the client side to not create
an instance of `E_Type`, which would not be meaningful.
I tried to have this:
type E_Type (<>) is private;
type W_Type
(Is_Null : Boolean)
is tagged record
case Is_Null is
when True => null;
when False => E : not null access E_Type;
end case;
end record;
function F return W_Type'Class;
Then, a type derived from `W_Type` extends it with an added `E_Type`
aliased member… I bet you guess I would like to be able to assign `E` a
reference to that member, but that's a no way: compiling the specification
is OK, but compiling any implementation fails.
Should I give up with this and use a precondition instead?
What a pity we can't have a structure referencing it's own component. That
would be perfectly valid from an access point of view, but we can't use a
declaration before it's terminated, so access discriminant does not help
here, as what is required, is an access to an inner element:
X : X_Type
(...
E_Object => (...),
E => X.E_Object'Access); -- Fails
It happens I miss we could do that (providing `X_Type` would be a limited
type, of course).
--
“Syntactic sugar causes cancer of the semi-colons.” [1]
“Structured Programming supports the law of the excluded muddle.” [1]
[1]: Epigrams on Programming — Alan J. — P. Yale University
next reply other threads:[~2012-10-29 2:38 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2012-10-27 2:50 Yannick Duchêne (Hibou57) [this message]
2012-10-27 9:15 ` Conditional holder for a type with unknown discriminant AdaMagica
2012-10-27 12:03 ` Yannick Duchêne (Hibou57)
2012-10-27 17:17 ` AdaMagica
2012-10-27 19:14 ` gautier_niouzes
2012-10-29 7:22 ` Yannick Duchêne (Hibou57)
replies disabled
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox