erg-lang / erg

A statically typed language compatible with Python
http://erg-lang.org
Apache License 2.0
2.7k stars 55 forks source link

Add homogenous dict types #499

Open mtshiba opened 8 months ago

mtshiba commented 8 months ago

Currently, Erg doesn't have homogenous dict types.

dic: {Int: Str, Str: Float} = {1: "a", "b": 1.0}

A dictionary with Str as keys and Int as values is simply of type {Str: Int}.

We can write key/values of different types, but sometimes this spec is too elastic (especially for mutable Dict types).

Therefore, we propose to introduce homogenous dict types Dict(K, V)/Dict!(K, V).

dic: Dict(Str, Int) = {"a": 1, "b": 2}

The current heterogeneous dict types are kinds of structural types, just like record types, and are subtypes of the homogenous dict types. For example, {K1: V1, K2: V2} is a subtype of Dict(K1 or V1, K2 or V2).