c# - Code contracts, forall and custom enumerable -
i using c# 4.0 , code contracts , have own custom gameroomcollection : ienumerable<gameroom>
.
i want ensure, no instances of gameroomcollection
ever contain null
value element. don't seem able this, though. instead of making general rule, have tried plain , simple example. allgamerooms
instance of gameroomcollection
.
private void setuplisteners(gameroom newgameroom) { contract.requires(newgameroom != null); //... } private void setuplisteners(model model) { contract.requires(model != null); contract.requires(model.allgamerooms != null); contract.assume(contract.forall(model.allgamerooms, g => g != null)); foreach (gameroom gameroom in model.allgamerooms) setuplisteners(gameroom);//<= warning: code contracts: requires unproven: newgameroom != null }
can see, why haven't proven, gameroom
not null
?
edit:
adding reference object before iterating not work either:
ienumerable<igameroom> gamerooms = model.allgamerooms; contract.assume(contract.forall(gamerooms, g => g != null)); foreach (igameroom gameroom in gamerooms) setuplisteners(gameroom);//<= warning: code contracts: requires unproven: newgameroom != null
edit2:
however: if convert game room collection type array, works fine:
igameroom[] gameroomarray = model.allgamerooms.toarray(); contract.assume(contract.forall(gameroomarray, g => g != null)); foreach (igameroom gameroom in gameroomarray) setuplisteners(gameroom);//<= no warning
is caused fact, cannot define rule methods of ienumerable<t>
interface?
edit3: can problem somehow related this question?
i think might have purity of getenumerator method. pureattribute
contracts accept methods defined [pure] (side effect free).
some information code contracts, purity
qoute:
purity
all methods called within contract must pure; is, must not update preexisting state. pure method allowed modify objects have been created after entry pure method.
code contract tools assume following code elements pure:
methods marked pureattribute.
types marked pureattribute (the attribute applies type's methods).
property accessors.
operators (static methods names start "op", , have 1 or 2 parameters , non-void return type).
any method qualified name begins "system.diagnostics.contracts.contract", "system.string", "system.io.path", or "system.type".
any invoked delegate, provided delegate type attributed pureattribute. delegate types system.predicate , system.comparison considered pure.
Comments
Post a Comment