mirror of
https://github.com/nushell/nushell.git
synced 2025-08-09 17:15:02 +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:
@ -7,16 +7,18 @@ use nu_protocol::{
|
||||
pub fn type_compatible(lhs: &Type, rhs: &Type) -> bool {
|
||||
// Structural subtyping
|
||||
let is_compatible = |expected: &[(String, Type)], found: &[(String, Type)]| {
|
||||
// the expected type is `any`
|
||||
if expected.is_empty() {
|
||||
true
|
||||
} else if expected.len() != found.len() {
|
||||
} else if expected.len() > found.len() {
|
||||
false
|
||||
} else {
|
||||
expected
|
||||
.iter()
|
||||
.zip(found.iter())
|
||||
.all(|(lhs, rhs)| lhs.0 == rhs.0 && type_compatible(&lhs.1, &rhs.1))
|
||||
expected.iter().all(|(col_x, ty_x)| {
|
||||
if let Some((_, ty_y)) = found.iter().find(|(col_y, _)| col_x == col_y) {
|
||||
type_compatible(ty_x, ty_y)
|
||||
} else {
|
||||
false
|
||||
}
|
||||
})
|
||||
}
|
||||
};
|
||||
|
||||
|
Reference in New Issue
Block a user