kmruiz/sonata

Generics

开放

#2 创建于 2020年2月26日

 (0 条评论) (0 个反应) (0 位负责人)C++ (0 个派生)auto 404
compilerdiscoveryhelp wantedlanguage

仓库指标

星标
 (25 个星标)
PR 合并指标
 (PR 指标待抓取)

描述

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.

贡献者指南