Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add set() for creating an empty set #2779

Merged
merged 2 commits into from
Jul 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you forgot to call the function. :-).


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
Loading