Skip to content

Commit c58d31c

Browse files
authored
Merge pull request #2779 from advikkabra/empty-set
Add `set()` for creating an empty set
2 parents e8d244e + bab03ba commit c58d31c

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
def test_empty_set():
2+
a: set[i32] = set()
3+
assert len(a) == 0
4+
a.add(2)
5+
a.remove(2)
6+
a.add(3)
7+
assert a.pop() == 3
8+
9+
b: set[str] = set()
10+
11+
assert len(b) == 0
12+
b.add('a')
13+
b.remove('a')
14+
b.add('b')
15+
assert b.pop() == 3
16+
17+
test_empty_set()

src/lpython/semantics/python_ast_to_asr.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8622,6 +8622,20 @@ we will have to use something else.
86228622
ASRUtils::type_to_str(type) + " type.", x.base.base.loc);
86238623
}
86248624
return;
8625+
} else if( call_name == "set" ) {
8626+
parse_args(x, args);
8627+
if (args.size() == 0) {
8628+
if( assign_asr_target != nullptr ) {
8629+
tmp = ASR::make_SetConstant_t(al, x.base.base.loc, nullptr, 0,
8630+
ASRUtils::expr_type(assign_asr_target));
8631+
}
8632+
else {
8633+
tmp = nullptr;
8634+
}
8635+
return ;
8636+
}
8637+
8638+
throw SemanticError("set is only used for an empty set for now.", x.base.base.loc);
86258639
} else if( call_name == "deepcopy" ) {
86268640
parse_args(x, args);
86278641
if( args.size() != 1 ) {

0 commit comments

Comments
 (0)