`new Set()` or `new Map()` with more than one argument

# Axel Rauschmayer (10 years ago)

I’ve accidentally created the wrong set a few times:

let set = new Set('red', 'green', 'blue');
    // WRONG: same as new Set(['r', 'e', 'd'])

Would it make sense to throw if either of the constructors Set and Map receives more than one argument?

# Allen Wirfs-Brock (10 years ago)

or perhaps we should have Set.of(...args) and Set.from(iterable) methods.

# Rick Waldron (10 years ago)

On Tue Feb 24 2015 at 10:48:59 AM Allen Wirfs-Brock <allen at wirfs-brock.com> wrote:

or perhaps we should have Set.of(...args) and Set.from(iterable) methods.

+1

Also, it occurs to me that specifying Set and Map to throw when there is greater than 1 argument would provide insurance for the comparator function argument. Sometime in the future, the operation could be relaxed to allow accepting that second argument.

# Mark S. Miller (10 years ago)

As always with proposals to extend arity -- even if reserved by a thrown error in a previous release -- how would you feature test for the extended functionality?

I suspect the awkwardness of feature testing is one of the reasons why we have not previously added new functionality by extending arity of existing std functions. Though reserving by throwing does change the game somewhat. Does it change the game enough?

# Rick Waldron (10 years ago)

TBH, I've never encountered the issue that Dr. Rauschmayer reported—so I'm not convinced that it's necessary to do anything about it at all. I only made the suggestion because I remembered the the user-defined comparator, and figured that a thrown exception would prevent any code, that accidentally relied on args > 1 being silently ignored, from coming into

existence. So forget that, because you're right and this sucks:

  var m;
  try {
    m = new Map(..., comparator);
  } catch (e) {
    m = new Map(...);
  }

No thanks to that—I'd rather risk the rare cases in which args > 1 are silently ignored and bugs hopefully caught by well written tests.

# Tab Atkins Jr. (10 years ago)

On Tue, Feb 24, 2015 at 10:06 AM, Rick Waldron <waldron.rick at gmail.com> wrote:

TBH, I've never encountered the issue that Dr. Rauschmayer reported—so I'm not convinced that it's necessary to do anything about it at all.

Python has the same issue, and I've run into it before (exactly the same examples - passing several strings, and unexpectedly getting a Set of characters from the first string), but it's not too hard to train yourself to always pass a list to Set. I agree that we probably don't need to do anything to fix this.

# Leon Arnott (10 years ago)

or perhaps we should have Set.of(...args) and Set.from(iterable) methods.

I was under the impression that Array.of() was solely an apologetic patch over Array()'s broken arguments interpretation, and not really an apogee for future or present collection classes.

Regarding the main topic: I personally this case would be better solved by adding one of those Set and Map literal ideas that were kicked around awhile back and using those in lieu of the clunky constructor.