From: "Yannick Duchêne (Hibou57)" <yannick_duchene@yahoo.fr>
Subject: Design by contract and control inversion
Date: Wed, 31 Oct 2012 20:28:30 +0100
Date: 2012-10-31T20:28:30+01:00 [thread overview]
Message-ID: <op.wm15ps0cule2fv@cardamome> (raw)
Hi all,
I wondered if there are known idioms to express predicates for callbacks,
which may be via access to subprogram or interface/tagged types. Control
inversion seems to not be easily mixable with design by contract. An
example to make it clearer.
Say a type `T` has a method with expect either an access to subprogram or
an interface:
type T is private;
procedure Expect_Handler
(Me : T;
Handler : not null access
procedure (Context : T));
type H is interface;
procedure Handle
(Me : H; Context : T)
is abstract;
procedure Expect_Handler
(Me : T;
Handler : H'Class);
Now let say the type `T` has different properties, and one is always
`True` when no handler in currently invoked, and always `False` when an
handler is currently running (which may be nesting), so that from inside
of any `Handle` interface method of subprogram, then that property will
always be `False`, and that I want it to be part of the specification.
I can't make it a precondition for the `Handle` method of type `H`, that
would not be clean and not what's really intended. Worst, I can't express
anything at all with the access to subprogram case.
The only thing I could imagine, is to create a second type, `U`,
re‑interfacing `T`, and passed to the handlers instead of `T`:
type T is private;
type U (<>) is limited private;
procedure Expect_Handler
(Me : T;
Handler : not null access
procedure (Context : U));
type H is interface;
procedure Handle
(Me : H; Context : U)
is abstract;
procedure Expect_Handler
(Me : T;
Handler : H'Class);
And in the private part, one of `U` or `T` wraps the other (within a
record). Not that clean, but at least, `T` and `U` can expose different
properties, and `U` is only used to be passed as parameters to handlers.
This construct is not only interesting for design by contract, as it also
allows to expose a different subprogram set for both type; as an example,
in the real thing I did, `U` only gets a subset of the subprograms
applicable to `T`.
Do you believe that's the best way to do? Or is this too much tricky and
you know another idiom?
--
“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-31 19:28 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
2012-10-31 19:28 Yannick Duchêne (Hibou57) [this message]
2012-11-01 17:13 ` Design by contract and control inversion Yannick Duchêne (Hibou57)
2012-11-01 20:29 ` Adam Beneschan
2012-11-02 3:40 ` Yannick Duchêne (Hibou57)
2012-11-02 8:59 ` Yannick Duchêne (Hibou57)
2012-11-02 12:32 ` Yannick Duchêne (Hibou57)
2012-11-07 1:34 ` Yannick Duchêne (Hibou57)
2012-11-02 16:45 ` Shark8
2012-11-07 1:51 ` 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