allow tables in ++ operator (#7051)

This PR closes #6916, which now allows table/table operations on the
`++` operator.
```
[[a b]; [1 2]] ++ [[a b]; [1 3]]
╭───┬───┬───╮
│ # │ a │ b │
├───┼───┼───┤
│ 0 │ 1 │ 2 │
│ 1 │ 1 │ 3 │
╰───┴───┴───╯
```
This commit is contained in:
David Matos 2022-12-01 00:21:59 +01:00 committed by GitHub
parent b7e5790cd1
commit fd503fceaf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 12 additions and 1 deletions

View File

@ -509,3 +509,14 @@ fn adding_value_and_list() {
assert_eq!(actual.out, "[1, 3, 5]"); assert_eq!(actual.out, "[1, 3, 5]");
} }
#[test]
fn adding_tables() {
let actual = nu!(
cwd: "tests/fixtures/formats", pipeline(
r#"
[[a b]; [1 2]] ++ [[4 5]; [10 11]] | to nuon
"#
));
assert_eq!(actual.out, "[{a: 1, b: 2}, {4: 10, 5: 11}]");
}

View File

@ -23,7 +23,6 @@ pub fn math_result_type(
op: &mut Expression, op: &mut Expression,
rhs: &mut Expression, rhs: &mut Expression,
) -> (Type, Option<ParseError>) { ) -> (Type, Option<ParseError>) {
//println!("checking: {:?} {:?} {:?}", lhs, op, rhs);
match &op.expr { match &op.expr {
Expr::Operator(operator) => match operator { Expr::Operator(operator) => match operator {
Operator::Math(Math::Plus) => match (&lhs.ty, &rhs.ty) { Operator::Math(Math::Plus) => match (&lhs.ty, &rhs.ty) {
@ -84,6 +83,7 @@ pub fn math_result_type(
(Type::List(Box::new(Type::Any)), None) (Type::List(Box::new(Type::Any)), None)
} }
} }
(Type::Table(a), Type::Table(_)) => (Type::Table(a.clone()), None),
_ => { _ => {
*op = Expression::garbage(op.span); *op = Expression::garbage(op.span);
( (