comp.lang.ada
 help / color / mirror / Atom feed
* Design by contract and control inversion
@ 2012-10-31 19:28 Yannick Duchêne (Hibou57)
  2012-11-01 17:13 ` Yannick Duchêne (Hibou57)
                   ` (2 more replies)
  0 siblings, 3 replies; 9+ messages in thread
From: Yannick Duchêne (Hibou57) @ 2012-10-31 19:28 UTC (permalink / 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



^ permalink raw reply	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2012-11-08  5:39 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2012-10-31 19:28 Design by contract and control inversion Yannick Duchêne (Hibou57)
2012-11-01 17:13 ` 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)

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox