[Issue 6857] Precondition contract checks should be statically bound.
d-bugmail at puremagic.com
d-bugmail at puremagic.com
Sun Jul 8 14:58:17 PDT 2012
http://d.puremagic.com/issues/show_bug.cgi?id=6857
--- Comment #72 from timon.gehr at gmx.ch 2012-07-08 14:58:07 PDT ---
(In reply to comment #70)
> I must say, I would be entirely opposed to fixing this "bug". As far as I can
> see, the arguments of Timon and deadalnix have been entirely theoretical,
It is irrelevant who made the arguments.
> whereas it's not that hard to envision a situation where the user would fully
> desire and expect the wider contract of B to apply even though the static type
> is A.
>
> Suppose that D did check *only* A's contract when calling into an A (that might
> be a B). I bet that if contracts were widely and exactingly used, cases where
> this causes trouble would pop up frequently. The main problem, I think, are
> low-level "deputies" that provide functionality for higher-level clients.
>
> For example, consider a collection that only allows items that match certain
> constraints, but the constraints are eliminated in B:
>
> // BTW I wanted to use an interface here, but I get
> // "missing body { ... } after in or out"
> // and then if I add a body, D complains that it is not abstract!
Yah, that's a long standing bug.
> class A {
> abstract void add(Item item)
> in { assert(item.x > 0); } body {}
> }
> class B : A {
> override void add(Item item) in {} body {}
> }
> struct Item { int x; }
>
> Now suppose I write a function that helpfully adds a series of items to an A:
>
> void addAll(A set, Item[] arr...) {
> foreach(Item x; arr)
> set.add(x);
> }
>
This function is clearly in error. You could do it like this:
class A {
abstract void add(Item item) in { assert(validate(item)); }
bool validate(Item item) const pure { return item.x > 0; }
}
class B : A {
override void add(Item item) {}
override bool validate(Item item) const pure { return true; }
}
void addAll(A set, Item[] arr...)in{
foreach(x; arr) assert(set.validate(x));
}body{
foreach(x; arr) set.add(x);
}
--
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
More information about the Digitalmars-d-bugs
mailing list