Custom set: should we note the set is supposed to be immutable?

In custom-set the add function is supposed to return a Set.

I implemented that as add the element to my dict of elements and simply return self.

Then I noticed your solution where you create a new Set with my elements plus the new one.

Should we add an instructions.append specifying which behaviour to use?

Likely a post for a separate thread, but I find the instructions incredibly vague.

"Create a custom set type.

Sometimes it is necessary to define a custom data structure of some type, like a set. In this exercise you will define your own set. How it works internally doesn’t matter, as long as it behaves like a set of unique elements."

We never explain what a set is and tell you to implement it without saying how one works. I know the test suite is the source of truth for what your implementation needs to do, but if I didn’t know what a set was beforehand, I’d be pretty lost.

If we specify which behavior to use, we should also test to ensure that behavior is followed.

If we follow the semantics of Python’s builtin set, then set.add() would mutate the set and would not return a set. It would be easy to test for a mutation by ignoring the return value.

If we follow the pattern of the other operations (union, intersection, difference), the set should not be mutated.

Is one clearly more correct than the other? If not, is there any reason to limit the implementation? It can make for a good mentor discussion topic either way.

Re: description, that’s a problem spec discussion, not a GDScript discussion :slight_smile: The description could benefit from a rewrite (ideally with an intro and instruction) … but not in this category.

1 Like

Could the analyzer pick up on whether the set is mutable and suggest making it immutable (or vice versa if we go that way)? Leaving it for a mentoring discussion is fine, but more people would potentially see the analyzer feedback than do a mentoring discussion.

…probably. The analyzer isn’t yet live so I haven’t yet looked into building custom reports. But if one approach isn’t more correct than the other, why would we suggest using one over the other?