invariants

What does Liskov Substitution Principle preserve? [duplicate]

独自空忆成欢 提交于 2019-12-11 20:10:56
问题 This question already has answers here : What is an example of the Liskov Substitution Principle? (29 answers) Closed 7 months ago . As I've read the substitution of objects of a concrete type by instances of a subclass of that concrete type must preserve a program's correctness, a program's invariants. I'd like to know what exactly is meant by correctness and invariants of a program? 回答1: Let's say you have a class Animal and someone asks you what it's supposed to do, what it can be used for

Find Maximum Value in Array in Ada

别来无恙 提交于 2019-12-11 07:58:53
问题 I am doing an Ada program with lots of different functions messing with arrays, i got all my sorting functions going, i am now stuck on retrieving the maximum value in an array using a loop invariant to design the loop for that function. any help? 回答1: How about simply looping over the whole array? something like this: function Get_Maximum (Of : My_Array_Type) return Element_Type is Maximum : Element_Type := Of (Of'First); begin for I in Of'First + 1 .. Of'Last loop if Of (I) > Maximum then

How to maintain a recursive invariant in a MySQL database?

你说的曾经没有我的故事 提交于 2019-12-11 03:08:23
问题 I have a tree encoded in a MySQL database as edges: CREATE TABLE items ( num INT, tot INT, PRIMARY KEY (num) ); CREATE TABLE tree ( orig INT, term INT FOREIGN KEY (orig,term) REFERENCES items (num,num) ) For each leaf in the tree, items.tot is set by someone. For interior nodes, items.tot needs to be the sum of it's children. Running the following query repeatedly would generate the desired result. UPDATE items SET tot = ( SELECT SUM(b.tot) FROM tree JOIN items AS b ON tree.term = b.num WHERE

Can an aggregates invariant include a rule based on information from elsewhere?

徘徊边缘 提交于 2019-12-09 16:04:32
问题 In DDD can an aggregates invariant include a rule based on information in a another aggregate? Now I don't think so, however this causes me a problem and I don't know how to solve it. I have an entity called Asset (equipment) which I'm modelling as the root of an aggregate. It has a list of Tags (properties) that describe things like Manufacturer, Model etc. It stores the identity of second aggregate called AssetType which has a list of TagTypes, of which some can be marked as mandatory. Now

Are preconditions and postconditions needed in addition to invariants in member functions if doing design by contract?

好久不见. 提交于 2019-12-07 09:34:05
问题 I understand that in the DbC method, preconditions and postconditions are attached to a function. What I'm wondering is if that applies to member functions as well. For instance, assuming I use invariants at the beginning at end of each public function, a member function will look like this: edit: (cleaned up my example) void Charcoal::LightOnFire() { invariant(); in_LightOnFire(); StartBurning(); m_Status = STATUS_BURNING; m_Color = 0xCCCCCC; return; // last return in body out_LightOnFire();

Immutable class in Eiffel

久未见 提交于 2019-12-07 04:03:50
问题 I'm trying to make an immutable POINT class in Eiffel. Is the code below defines one? The {NONE} accessibility for the x and y fields is enough for it? Can I write something to the class invariant like x = x' , or how else can I achieve immutability? class POINT create make feature {NONE} x: DOUBLE y: DOUBLE feature make (x_: DOUBLE; y_: DOUBLE) do x := x_ y := y_ ensure set: x = x_ and y = y_ end feature --accessors get_x: DOUBLE do Result := x ensure Result = x end end 回答1: Eiffel does not

Code Contracts: Invariants in abstract class

淺唱寂寞╮ 提交于 2019-12-05 19:18:14
问题 I have encountered a problem while using Invariants with Code Contracts. I want to define an Invariant within my abstract class but it is simply ignored. The code below shows my interface and the abstract class. [ContractClass(typeof(IPointContract))] interface IPoint { int X { get; } int Y { get; } } [ContractClassFor(typeof(IPoint))] abstract class IPointContract : IPoint { public int X { get { return 0; } } public int Y { get { return 0; } } [ContractInvariantMethod] private void

Are preconditions and postconditions needed in addition to invariants in member functions if doing design by contract?

落爺英雄遲暮 提交于 2019-12-05 15:55:13
I understand that in the DbC method, preconditions and postconditions are attached to a function. What I'm wondering is if that applies to member functions as well. For instance, assuming I use invariants at the beginning at end of each public function, a member function will look like this: edit: (cleaned up my example) void Charcoal::LightOnFire() { invariant(); in_LightOnFire(); StartBurning(); m_Status = STATUS_BURNING; m_Color = 0xCCCCCC; return; // last return in body out_LightOnFire(); invariant(); } inline void Charcoal::in_LightOnFire() { #ifndef _RELEASE_ assert (m_Status == STATUS

covariant type T occurs in invariant position

筅森魡賤 提交于 2019-12-05 02:19:13
I'm moving my first steps in Scala and I would like to make the following code works: trait Gene[+T] { val gene: Array[T] } The error that the compiler gives is: covariant type T occurs in invariant position in type => Array[T] of value gene I know I could do something like: trait Gene[+T] { def gene[U >: T]: Array[U] } but this doesn't solve the problem because I need a value: pratically what I'm trying to say is "I don't care of the inside type, I know that genes will have a gene field that return its content". (the +T here is because I wanna do something like type Genome = Array[Gene[Any]]

Class invariants in Python

天涯浪子 提交于 2019-12-04 21:00:45
问题 Class invariants definitely can be useful in coding, as they can give instant feedback when clear programming error has been detected and also they improve code readability as being explicit about what arguments and return value can be. I'm sure this applies to Python too. However, generally in Python, testing of arguments seems not to be "pythonic" way to do things, as it is against the duck-typing idiom. My questions are: What is Pythonic way to use assertions in code? For example, if I had