{"id":5099,"date":"2014-03-30T18:47:01","date_gmt":"2014-03-30T18:47:01","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/problem-about-invariants-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:42:27","modified_gmt":"2022-08-30T15:42:27","slug":"problem-about-invariants-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/problem-about-invariants-collection-of-common-programming-errors\/","title":{"rendered":"problem about invariants-Collection of common programming errors"},"content":{"rendered":"<ul>\n<li><img decoding=\"async\" src=\"http:\/\/www.gravatar.com\/avatar\/d8c4e8f004ecd0260a27b0b9040c8083?s=32&amp;d=identicon&amp;r=PG\" \/><br \/>\nIng\u00f3 Vals<br \/>\nstatic-typing invariants<br \/>\nOk not really sure if I&#8217;m right.I only recently learned that I needed to have contravariant interface to be able to pass that interface as a parameter in C# and this feature was only added in .NET 4.0.So obviously there is some reason you can&#8217;t do this Covariant or Invariant interfaces and it probably has something to do with passing in, getting out the generic class.I&#8217;m not really sure what the limitation here is, and know that I&#8217;ve seen where Contravariant interface can be used where the other<\/li>\n<li><img decoding=\"async\" src=\"http:\/\/i.stack.imgur.com\/D9tx3.jpg?s=32&amp;g=1\" \/><br \/>\nafsantos<br \/>\njava architecture methods constraints invariants<br \/>\nContext (Edit)Some clarification was on demand, so I&#8217;ll try to sum up what influences the question.The goal of the project is to provide a certain functionality to programmers, most probably in the form of a library (a JAR with class files, I guess). To use said functionality, programmers would have to conform to the constraints that must (should) be satisfied. Otherwise it won&#8217;t function as expected (just like the locks from java.util.concurrent, that must be acquired \/ freed at the appropriate<\/li>\n<li><img decoding=\"async\" src=\"http:\/\/www.gravatar.com\/avatar\/cc005e2b716b01120791e17d280bcdc9?s=32&amp;d=identicon&amp;r=PG\" \/><br \/>\nCody<br \/>\nc# code-contracts invariants<br \/>\nI&#8217;m trying to write this method using c# contracts&#8230;but when debugging, it completely ignores the Contract.requires and CheckRep() Am I using this incorrectly??public Poly Add(Poly q){CheckRep();Contract.Requires(q != null, &#8220;You need to provide a valid non-null Poly.&#8221;);Poly la, sm;if (deg &gt; q.deg){la = this; sm = q;}else{la = q; sm = this;}int newdeg = la.deg;if (deg == q.deg){for (int k = deg; k &gt; 0; k&#8211;){if (trms[k] + q.trms[k] != 0){break;}else{newdeg&#8211;;}}}Poly r = new Poly(newdeg);int<\/li>\n<li><img decoding=\"async\" src=\"http:\/\/www.gravatar.com\/avatar\/8a662b85e473c0c03c90a3a0ca42dff8?s=32&amp;d=identicon&amp;r=PG\" \/><br \/>\nMike Samuel<br \/>\npattern-matching ocaml invariants<br \/>\nI want to define a type so that all construction goes through module members that can preserve invariants, but allow destructuring for pattern matching.I&#8217;m just learning OCaml but the following almost works for an int pair with the invariant that the left should be strictly less than the rightmodule Range : sigtype t = private { left:int; right:int }exception InvalidRange of (int*int)val make : int -&gt; int -&gt; t end = structtype t = { left:int; right:int }exception InvalidRange of (int*int)l<\/li>\n<li><img decoding=\"async\" src=\"http:\/\/www.gravatar.com\/avatar\/082b8ee2b0aebb430b8d4d98e6a5c32f?s=32&amp;d=identicon&amp;r=PG\" \/><br \/>\nFredOverflow<br \/>\njava exception visibility runtime-error invariants<br \/>\nI want to throw a runtime exception in case my class invariants are invalidated. Since this is a programming error (similar to a NullPointerException), clients should not catch that exception.Should the exception class be declared private or public (or something else)?class Foo {\/\/ &#8230;private static class InvariantsViolated{\/\/ &#8230;} }Are there any guidelines on custom runtime exceptions and visibility?<\/li>\n<li><img decoding=\"async\" src=\"http:\/\/www.gravatar.com\/avatar\/ba627cf03ffef65c09ea6a77e4267d3b?s=32&amp;d=identicon&amp;r=PG\" \/><br \/>\nVincenzo Maggio<br \/>\nscala covariance invariants<br \/>\nI&#8217;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 =&gt; Array[T] of value geneI know I could do something like:trait Gene[+T] {def gene[U &gt;: T]: Array[U] }but this doesn&#8217;t solve the problem because I need a value: pratically what I&#8217;m trying to say is &#8220;I don&#8217;t care of the inside type, I know that genes will have a gene field that ret<\/li>\n<li><img decoding=\"async\" src=\"http:\/\/www.gravatar.com\/avatar\/1f5ce0fac6a8d23e6dced87ab200e669?s=32&amp;d=identicon&amp;r=PG\" \/><br \/>\nJohann Gerell<br \/>\nc# c++ validation invariants<br \/>\nI&#8217;m interested in hearing what technique(s) you&#8217;re using to validate the internal state of an object during an operation that, from it&#8217;s own point of view, only can fail because of bad internal state or invariant breach.My primary focus is on C++, since in C# the official and prevalent way is to throw an exception, and in C++ there&#8217;s not just one single way to do this (ok, not really in C# either, I know that).Note that I&#8217;m not talking about function parameter validation, but more like class inv<\/li>\n<li><img decoding=\"async\" src=\"http:\/\/www.gravatar.com\/avatar\/0c3fda5be62c1c1f0bc6f6a534273ac2?s=32&amp;d=identicon&amp;r=PG\" \/><br \/>\nPillsy<br \/>\nc++ c++11 move-semantics pimpl-idiom invariants<br \/>\nI&#8217;ve been trying to wrap my head around how move semantics in C++11 are supposed to work, and I&#8217;m having a good deal of trouble understanding what conditions a moved-from object needs to satisfy. Looking at the answer here doesn&#8217;t really resolve my question, because can&#8217;t see how to apply it to pimpl objects in a sensible way, despite arguments that move semantics are perfect for pimpls.The easiest illustration of my problem involves the pimpl idiom, like so:class Foo {std::unique_ptr&lt;FooImpl<\/li>\n<li><img decoding=\"async\" src=\"http:\/\/www.gravatar.com\/avatar\/063b717fe3648e2492c291f59663f7a4?s=32&amp;d=identicon&amp;r=PG\" \/><br \/>\ninvitapriore<br \/>\nobjective-c oop error-handling setter invariants<br \/>\nI&#8217;m new to Objective-C and trying to figure out what the best way of maintaining the rep invariant of a class is, given that exceptions aren&#8217;t really an appropriate way of enforcing them. A good example of where this would come up is in the Fraction class that serves as an example in Kochan&#8217;s Programming in Objective-C, which has this setter method:-(void) setDenominator: (int) d {self.denominator = d; }So say part of your rep invariant demands self.denominator != 0. In Java, for instance, the m<\/li>\n<li><img decoding=\"async\" src=\"http:\/\/www.gravatar.com\/avatar\/94e2dd813e278309b6281b4a4b7fdddd?s=32&amp;d=identicon&amp;r=PG\" \/><br \/>\ncuriousguy<br \/>\nc++ stl set const invariants<br \/>\nHaving the following code:#include &lt;iostream&gt; #include &lt;set&gt; #include &lt;string&gt; #include &lt;functional&gt;using namespace std;class Employee {\/\/ &#8230;int _id;string _name;string _title; public:Employee(int id): _id(id) {}string const &amp;name() const { return _name; }void setName(string const &amp;newName) { _name = newName; }string const &amp;title() const { return _title; }void setTitle(string const &amp;newTitle) { _title = newTitle; }int id() const { return _id; } };struct c<\/li>\n<li><img decoding=\"async\" src=\"http:\/\/www.gravatar.com\/avatar\/af7e921c45b16dfb05543640b847753e?s=32&amp;d=identicon&amp;r=PG\" \/><br \/>\nIsuru<br \/>\njava list subclass superclass invariants<br \/>\nI have following classespublic class Animalpublic class Dog extends Animalpublic class Cat extends AnimalAnd for the testing I have a driver class.public class Driver {public static void main(String[] args){List&lt;? extends Animal&gt; animalList = Arrays.&lt;Dog&gt;asList(new Dog(), new Dog(), new Dog());animalList.add(new Dog()) \/\/ Compilation error } }By default list are invariant type containers. For example say we have List&lt;Object&gt; objectList, ArrayList&lt;String&gt;<\/li>\n<\/ul>\n<p>Web site is in building<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Ing\u00f3 Vals static-typing invariants Ok not really sure if I&#8217;m right.I only recently learned that I needed to have contravariant interface to be able to pass that interface as a parameter in C# and this feature was only added in .NET 4.0.So obviously there is some reason you can&#8217;t do this Covariant or Invariant interfaces [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4,1],"tags":[],"class_list":["post-5099","post","type-post","status-publish","format-standard","hentry","category-semantic","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/5099","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=5099"}],"version-history":[{"count":1,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/5099\/revisions"}],"predecessor-version":[{"id":8779,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/5099\/revisions\/8779"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=5099"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=5099"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=5099"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}