Skip to content

Commit

Permalink
Merge pull request #2779 from advikkabra/empty-set
Browse files Browse the repository at this point in the history
Add `set()` for creating an empty set
  • Loading branch information
czgdp1807 authored Jul 21, 2024
2 parents e8d244e + bab03ba commit c58d31c
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
17 changes: 17 additions & 0 deletions integration_tests/test_set_constructor.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
def test_empty_set():
a: set[i32] = set()
assert len(a) == 0
a.add(2)
a.remove(2)
a.add(3)
assert a.pop() == 3

b: set[str] = set()

assert len(b) == 0
b.add('a')
b.remove('a')
b.add('b')
assert b.pop() == 3

test_empty_set()
14 changes: 14 additions & 0 deletions src/lpython/semantics/python_ast_to_asr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8622,6 +8622,20 @@ we will have to use something else.
ASRUtils::type_to_str(type) + " type.", x.base.base.loc);
}
return;
} else if( call_name == "set" ) {
parse_args(x, args);
if (args.size() == 0) {
if( assign_asr_target != nullptr ) {
tmp = ASR::make_SetConstant_t(al, x.base.base.loc, nullptr, 0,
ASRUtils::expr_type(assign_asr_target));
}
else {
tmp = nullptr;
}
return ;
}

throw SemanticError("set is only used for an empty set for now.", x.base.base.loc);
} else if( call_name == "deepcopy" ) {
parse_args(x, args);
if( args.size() != 1 ) {
Expand Down

0 comments on commit c58d31c

Please sign in to comment.