Idea for CRUD types - feedback appreciated

This is less about your overall design but I just wanted to point out that you can do the following to ensure that the classes are correct when defined rather than deferring that check to one of the call sites.

type Type<T> = {
  get : () -> T;
  set : T -> ();
  sanitise : () -> T;
  validate : () -> Bool;
};

class FloatType() : Type<Float> {
  var value : Float = 0;
  public func get() : Float { value };
  public func set(v : Float) {};
  public func sanitise() : Float { value };
  public func validate() : Bool { true };
};

class IntType() : Type<Int> {
  var value : Int = 0;
  public func get() : Int { value };
  public func set(v : Int) {};
  public func sanitise() : Int { value };
  public func validate() : Bool { true };
};

For example, if you accidentally omitted the validate method from the IntType definition you would get this error:

main.mo:16.1-22.2: type error, class body of type
  {get : () -> Int; sanitise : () -> Int; set : Int -> ()}
does not match expected type
  Type<Int> = {get : () -> Int; sanitise : () -> Int; set : Int -> (); validate : () -> Bool}
2 Likes