compilerdiscoveryhelp wantedlanguage
Repository metrics
- Stars
- (25 stars)
- PR merge metrics
- (PR metrics pending)
Description
Context
Sonata does not implement, yet, generics.
Motivation
We need to support generics to ensure type safety in the communication between entities.
Requirements
Sonata is meant to be a simple language, so generics should be as easy as possible. We want to make sure the following constraints are considered:
- The generic type system should allow any reasonable need.
- Simplicity is key: if something would be complex, redesign or drop it.
- Should not break entity/value visibility constraints.
Approach
A suggested approach would be:
- Type inference should be smart enough to cover most of the reasonable cases where we use generics.
- Generic rules will be based on:
- value classes can not be inherited
- entity classes can not be inherited
- entity classes can implement contracts
So we have the following cases:
- Generic on anything (like collections)
- We will need to change the ContinuationProcessor to take into consideration generics that contain entity classes.
- Generics on contracts are covariant.
- Generics on entities and value classes are invariant.
Examples
; let's assume a new Bag class:
entity class Bag<T> {
let put(a: T) = {}
}
; If we use the Bag to store a contract, it will be covariant:
contract Toy {}
entity class Football implements Toy {}
entity class Basketball implements Toy {}
let toys = Bag<Toy>()
; If we use the Bag to store a football, it should work
toys.put(Football())
; If we have a bag to store footballs, only footballs are allowed
Bag<Football>().put(Basketball()) ; fails
Affected Modules
- The standard library needs to be changed to use generics instead of any.
- The compiler should understand generic types on method signatures.
- The compiler should understand generic types when building continuations.