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

correct set.union method #574

Merged
merged 3 commits into from
Jan 28, 2025
Merged

Conversation

andponlin-canva
Copy link
Contributor

The union function on set is inconsistent with the behaviour of update as it does not support multiple iterable positional arguments as is the case in the Bazel specification of the language. This PR will align starlark-go with the Bazel spec.

Note that the set.union method no longer uses the Union function defined in value.go in order to avoid making a new set instance for each interable processed.

The `union` function on `set` is inconsistent with the behaviour of
`update` as it does not support multiple iterable positional arguments
as is the case in the Bazel specification of the language. This PR
will align `starlark-go` with the Bazel spec.

Note that the `set.union` method no longer uses the `Union` function
defined in `value.go` in order to avoid making a new `set` instance
for each interable processed.
Copy link
Collaborator

@adonovan adonovan left a comment

Choose a reason for hiding this comment

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

Well spotted! Thanks for the fix.

starlark/library.go Outdated Show resolved Hide resolved
starlark/library.go Outdated Show resolved Hide resolved
defer iter.Done()
return resultSet.InsertAll(iter)
}(); err != nil {
return nil, err
Copy link
Collaborator

Choose a reason for hiding this comment

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

This should return nameErr, like set_update.

But instead let's factor the common code of set_update and set_union since they only differ by whether the destination is a clone:

func set_union(_ *Thread, b *Builtin, args Tuple, kwargs []Tuple) (Value, error) {
	if len(kwargs) > 0 {
		return nil, nameErr(b, "union does not accept keyword arguments")
	}
	res := b.Receiver().(*Set).clone()
  	if err := setUpdate(res, args); err != nil {
  	  	return nil, nameErr(b, err)
  	}
  	return res, nil
}

func set_update(_ *Thread, b *Builtin, args Tuple, kwargs []Tuple) (Value, error) {
	if len(kwargs) > 0 {
		return nil, nameErr(b, "update does not accept keyword arguments")
	}
	recv := b.Receiver().(*Set)
   	if err := setUpdate(res, args); err != nil {
  	  	return nil,nameErr(b, err)
  	}
  	return None, nil
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ah true; I will do that...

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I've also factored-out the check for the kwargs because the update / union prefix can be added later albeit with an additional colon in the message. This seems OK to me; let me know what you think.

doc/spec.md Outdated Show resolved Hide resolved
Copy link
Collaborator

@adonovan adonovan left a comment

Choose a reason for hiding this comment

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

LGTM. Thanks!

@adonovan adonovan merged commit d908c3e into google:master Jan 28, 2025
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants