Better record types (#350)

This commit is contained in:
JT 2021-11-19 17:30:27 +13:00 committed by GitHub
parent 88988dc9f4
commit ff43ca4d24
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 18 additions and 7 deletions

View file

@ -332,7 +332,7 @@ pub fn math_result_type(
(t, Type::List(u)) if type_compatible(t, u) => (Type::Bool, None),
(Type::Int | Type::Float, Type::Range) => (Type::Bool, None),
(Type::String, Type::String) => (Type::Bool, None),
(Type::String, Type::Record(_, _)) => (Type::Bool, None),
(Type::String, Type::Record(_)) => (Type::Bool, None),
(Type::Unknown, _) => (Type::Bool, None),
(_, Type::Unknown) => (Type::Bool, None),
@ -354,7 +354,7 @@ pub fn math_result_type(
(t, Type::List(u)) if type_compatible(t, u) => (Type::Bool, None),
(Type::Int | Type::Float, Type::Range) => (Type::Bool, None),
(Type::String, Type::String) => (Type::Bool, None),
(Type::String, Type::Record(_, _)) => (Type::Bool, None),
(Type::String, Type::Record(_)) => (Type::Bool, None),
(Type::Unknown, _) => (Type::Bool, None),
(_, Type::Unknown) => (Type::Bool, None),

View file

@ -17,7 +17,7 @@ pub enum Type {
List(Box<Type>),
Number,
Nothing,
Record(Vec<String>, Vec<Type>),
Record(Vec<(String, Type)>),
Table,
ValueStream,
Unknown,
@ -37,7 +37,15 @@ impl Display for Type {
Type::Float => write!(f, "float"),
Type::Int => write!(f, "int"),
Type::Range => write!(f, "range"),
Type::Record(cols, vals) => write!(f, "record<{}, {:?}>", cols.join(", "), vals),
Type::Record(fields) => write!(
f,
"record<{}>",
fields
.iter()
.map(|(x, y)| format!("{}: {}", x, y.to_string()))
.collect::<Vec<String>>()
.join(", "),
),
Type::Table => write!(f, "table"),
Type::List(l) => write!(f, "list<{}>", l),
Type::Nothing => write!(f, "nothing"),

View file

@ -181,9 +181,12 @@ impl Value {
Value::Date { .. } => Type::Date,
Value::Range { .. } => Type::Range,
Value::String { .. } => Type::String,
Value::Record { cols, vals, .. } => {
Type::Record(cols.clone(), vals.iter().map(|x| x.get_type()).collect())
}
Value::Record { cols, vals, .. } => Type::Record(
cols.iter()
.zip(vals.iter())
.map(|(x, y)| (x.clone(), y.get_type()))
.collect(),
),
Value::List { .. } => Type::List(Box::new(Type::Unknown)), // FIXME
Value::Nothing { .. } => Type::Nothing,
Value::Block { .. } => Type::Block,