Skip to content

Commit

Permalink
function-like named tuple initialization
Browse files Browse the repository at this point in the history
  • Loading branch information
borisbat committed Nov 2, 2024
1 parent 4d32675 commit fa87ec9
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 5 deletions.
12 changes: 7 additions & 5 deletions examples/test/misc/hello_world.das
Original file line number Diff line number Diff line change
Expand Up @@ -189,11 +189,13 @@ tuple Tup {

[export]
def test_tuples {
let a : tuple<int;float;string> = (1,2.,"3");
let b : tuple<int;float;string> = tuple(1,2.,"3");
let c : tuple<int;float;string> = tuple<Tup>(a=1,b=2.,c="3");
let e : tuple<int;float;string> = tuple<Tup>(uninitialized a=1); // a=1,b=0.0,c=""
let f : tuple<int;float;string>* = new tuple(1,2.,"3"); // on heap
let a : tuple<int;float;string> = (1,2.,"3"); debug(a);
let b : tuple<int;float;string> = tuple(1,2.,"3"); debug(b);
let c : tuple<int;float;string> = Tup(1,2.,"3"); debug(c);
let d : tuple<int;float;string> = Tup(a=1,b=2.,c="3"); debug(d);
let e : tuple<int;float;string> = tuple<Tup>(a=1,b=2.,c="3"); debug(e);
let f : tuple<int;float;string> = tuple<Tup>(uninitialized a=1); debug(f); // a=1,b=0.0,c=""
let g : tuple<int;float;string>* = new tuple(1,2.,"3"); debug(g); // on heap
}

///////////
Expand Down
13 changes: 13 additions & 0 deletions src/ast/ast_infer_type.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8154,8 +8154,21 @@ namespace das {
ecast->reinterpret = true;
ecast->alwaysSafe = true;
expr->aliasSubstitution.reset();
reportAstChanged();
return ecast;
}
if ( !expr->func ) {
auto aliasT = findAlias(expr->name);
if ( aliasT && aliasT->isTuple() ) {
auto mkt = make_smart<ExprMakeTuple>(expr->at);
mkt->type = make_smart<TypeDecl>(*aliasT);
for ( auto & arg : expr->arguments ) {
mkt->values.push_back(arg->clone());
}
reportAstChanged();
return mkt;
}
}
if ( func && !expr->func && func->isClassMethod && func->arguments.size()>=1 ) {
auto bt = func->arguments[0]->type;
if ( bt && bt->isClass() ) {
Expand Down

0 comments on commit fa87ec9

Please sign in to comment.