mirror of
https://github.com/nushell/nushell.git
synced 2025-08-09 02:24:58 +02:00
improve subtyping (#9614)
# Description the current subtyping rule needs you to define the record entries in the same order as declared in the annotation. this pr improves that now ```nushell { name: 'Him', age: 12 } # , { age: 100, name: 'It' } # and { name: 'Red', age: 69, height: "5-8" } # will all match record<name: string, age: int> # previously only the first one would match ``` however, something like ```nushell { name: 'Her' } # will not # and { name: 'Car', wheels: 5 } ``` EDIT: applied JT's suggestion
This commit is contained in:
@ -41,12 +41,16 @@ impl Type {
|
||||
let is_subtype_collection = |this: &[(String, Type)], that: &[(String, Type)]| {
|
||||
if this.is_empty() || that.is_empty() {
|
||||
true
|
||||
} else if this.len() != that.len() {
|
||||
} else if this.len() > that.len() {
|
||||
false
|
||||
} else {
|
||||
this.iter()
|
||||
.zip(that.iter())
|
||||
.all(|(lhs, rhs)| lhs.0 == rhs.0 && lhs.1.is_subtype(&rhs.1))
|
||||
this.iter().all(|(col_x, ty_x)| {
|
||||
if let Some((_, ty_y)) = that.iter().find(|(col_y, _)| col_x == col_y) {
|
||||
ty_x.is_subtype(ty_y)
|
||||
} else {
|
||||
false
|
||||
}
|
||||
})
|
||||
}
|
||||
};
|
||||
|
||||
|
Reference in New Issue
Block a user