sergiotaborda / lense-lang

The Lense Language Compiler
1 stars 0 forks source link

Allow super types #80

Closed sergiotaborda closed 1 year ago

sergiotaborda commented 3 years ago

Super types are the types of claasses. they are like static interfaces/traits. they establish a contract with the class, not with the instance


public type class AdditionMonoid<T> 
          given T is AdditionMonoid<T>
{
       public plus (a :T, b:T) : T;
       public zero():T
}

public interface Integer satisfies AdditionMonoid<Integer> {
        public satisfy zero() => 0;
        public satisfy plus (a,b) => a.plus(b);

        public plus (a: Integer) : Integer;
}

// define based on type

public enhancement SumSequence extends Sequence<T> given T is AdditionMonoid<T>{

        public sum() {
               return this.reduce(T.zero(), (x,y) => T.plus(x,y));
         }
}

//use

assert (21, [1,2,3,4,5,6].sum());
sergiotaborda commented 1 year ago

Implemented outside SDK. Next step , use type classes to dine operators