tc39/proposal-symbol-predicates

throw on non-Symbols

michaelficarra opened this issue · 12 comments

I don't think these predicates should produce a Boolean result for non-Symbols. Receiving a false implies that we could answer the question but the question is, in fact, unanswerable.

Predicate functions don't typically throw on any input - if it's not a symbol, then logically it's not a well-known symbol - how is the question unanswerable?

Some existing precedent is Array.isArray, which never throws for any input.

(If we went with prototype methods, or accessors, then I would definitely expect throwing if the receiver isn't a symbol)

Ah, I was thinking of the question "is this Symbol well-known", not "is this a Symbol that is well-known" (probably because the name is just isWellKnown instead of isWellKnownSymbol). You're right that the former question is probably more expected of a prototype method. This is fine, then. Personally, I would probably rename it to isWellKnownSymbol to make the implication stronger.

It already is named that imo, since it’s on “Symbol” - its common use will have the words “Symbol is well known”, which would make that name redundant. We can certainly discuss it in plenary tho.

The "Symbol is well-known" reading is exactly why I thought it should throw for non-Symbols.

I think the difference here is whether the "is" is followed by a noun or an adjective. Array.isArray is fine. Array.isEmpty would need to throw on non-Arrays. However, Array.isEmptyArray could just return false for non-Arrays.

I see your logic, but i don’t agree with it; a predicate should never throw. I’d expect Array.isEmpty to have an implied “is array” as well.

It could return undefined instead of false in these cases, though, since that’s falsy.

The proposal achieved stage 2 today. Are you content with returning false for non-symbols, or is this a strongly held opinion?

I think this is something I'd like to hear opinions on from the broader committee.

Gotcha, then I should have asked that today. I'll see if I can squeeze in an overflow item.

@ljharb You can try sharing the thread on Matrix first or something.

good call, i'll do both!

ljharb commented

Rough consensus in plenary today was to keep them non-throwing, but to rename them to Symbol.isWellKnownSymbol and Symbol.isRegisteredSymbol.