problem about invariants-Collection of common programming errors
Ingó Vals
static-typing invariants
Ok not really sure if I’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’t do this Covariant or Invariant interfaces and it probably has something to do with passing in, getting out the generic class.I’m not really sure what the limitation here is, and know that I’ve seen where Contravariant interface can be used where the other
afsantos
java architecture methods constraints invariants
Context (Edit)Some clarification was on demand, so I’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’t function as expected (just like the locks from java.util.concurrent, that must be acquired / freed at the appropriate
Cody
c# code-contracts invariants
I’m trying to write this method using c# contracts…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, “You need to provide a valid non-null Poly.”);Poly la, sm;if (deg > q.deg){la = this; sm = q;}else{la = q; sm = this;}int newdeg = la.deg;if (deg == q.deg){for (int k = deg; k > 0; k–){if (trms[k] + q.trms[k] != 0){break;}else{newdeg–;}}}Poly r = new Poly(newdeg);int
Mike Samuel
pattern-matching ocaml invariants
I want to define a type so that all construction goes through module members that can preserve invariants, but allow destructuring for pattern matching.I’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 -> int -> t end = structtype t = { left:int; right:int }exception InvalidRange of (int*int)l
FredOverflow
java exception visibility runtime-error invariants
I 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 {// …private static class InvariantsViolated{// …} }Are there any guidelines on custom runtime exceptions and visibility?
Vincenzo Maggio
scala covariance invariants
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 geneI 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 ret
Johann Gerell
c# c++ validation invariants
I’m interested in hearing what technique(s) you’re using to validate the internal state of an object during an operation that, from it’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’s not just one single way to do this (ok, not really in C# either, I know that).Note that I’m not talking about function parameter validation, but more like class inv
Pillsy
c++ c++11 move-semantics pimpl-idiom invariants
I’ve been trying to wrap my head around how move semantics in C++11 are supposed to work, and I’m having a good deal of trouble understanding what conditions a moved-from object needs to satisfy. Looking at the answer here doesn’t really resolve my question, because can’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<FooImpl
invitapriore
objective-c oop error-handling setter invariants
I’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’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’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
curiousguy
c++ stl set const invariants
Having the following code:#include <iostream> #include <set> #include <string> #include <functional>using namespace std;class Employee {// …int _id;string _name;string _title; public:Employee(int id): _id(id) {}string const &name() const { return _name; }void setName(string const &newName) { _name = newName; }string const &title() const { return _title; }void setTitle(string const &newTitle) { _title = newTitle; }int id() const { return _id; } };struct c
Isuru
java list subclass superclass invariants
I 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<? extends Animal> animalList = Arrays.<Dog>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<Object> objectList, ArrayList<String>
Web site is in building