Later language changes donât use it, like Pythonâs type generics (typing.Generic), or Câs _BitInt(n). To use angle brackets today, U would need a very strong justification.
I beg to differ. Itâs only three characters longer. It requires no new syntax to be created and remembered. And itâs not only readable, but beautifully self-explanatory.
And itâs vulnerable to accidental shadowing of the name set, which is not the case for any other type.
Any proposal can be dismissed as merely âthree characters shorter/longerâ but thatâs an irrelevant criticism. (Unless someone is actually advocating for this purely on the basis of it being shorter, which hasnât happened yet.) We donât value code on the basis of length, which means we also shouldnât criticize code on the basis of length. Expressiveness and consistency are much more important.
I still really like my {/} idea from one of the many previous threads, it looks like the empty set from math if you squint! But Iâm fine with not having a language consteuct for this.
First, this breaks the current convention of using angle brackets for unrepresentable repr strings. Like, repr(42) is 42 and repr(datetime.date(2020, 12, 31)) is datetime.date(2020, 12, 31) because thatâs the code to re-create those objects.
But sqlite3.connect('example.db') is <sqlite3.Connection object at 0x00000265674545E0> with angle brackets specifically because you canât easily re-create a sqlite Connection object with a literal.
Second, this is the sort of âPERLificationâ change that makes the language less readable by trading words for punctuation symbols. This is a small change, but there are thousands of small changes like this that could be made and would result in a language full of cryptic-looking symbols.
Third, for example, if this change were adopted people would also want another change for frozen sets. How would you do this? Double angle brackets? So <> is a set and <<>> is a frozen set? Or should it be <<>> is a set and <> is a frozen set? You can see how this makes code unreadable.
EDIT: And what if you wanted an expression as part of the set? For example, you could have the dictionary {'of_age': age > 18}, but if you wanted to do that with this syntax, youâd end up with <age > 18>
Another issue with angle brackets is that it would be inconsistent with set literals and comprehensions, which use curly brackets. It would be odd to use <> for an empty set and {1} for a single-element one.
A less concrete issue is that it precludes angle brackets being used in the future for something more useful than this (I think this is fairly low-impact).
Personally I would prefer to deprecate [], {}, and () as a way of initializing and empty list, dict, and tuple. As a general rule I initialize them with calls to list(), dict(), and tuple() to make sure there is no confusion.
As to comprehensions, {n for n in range(10)} is clearly a set and {str(n): n for n in range(10)} is clearly a dict. I donât think there is any benefit in changing/adding a new way to do a set comprehension.
Do you also construct an empty string with str() rather than using the literal "" ? This all seems quite unnecessary - literals [1] exist for a reason. They are not confusing once you know that square brackets make a list.
If anything, the distinction between a set comprehension and a dict comprehension is far slimmer; so if that is sufficient, there should be no confusion with [] for a new empty list.
and âdisplayâ syntax which isnât technically a literal âŠď¸
I agree with you, but what should be the syntax? I really donât like to have alternative brackets for set, and I canât imagine a different solution.
The answer here is likely that if Python was redesigned today {} would be an empty set and {:} would be an empty dict, but we cannot do that anymore so sets will just have to be a bit less ergonomic than dicts. {*()} is a reasonable workaround.
List/array literals are in pretty much every language. I was responding to @brass75 's idea that [] is worse than list() which IMO is very much backwards.
We have syntax for everything except the empty set. I would be all in favour of an âempty set displayâ dedicated syntax, if a good one can be found; having these sorts of things as syntax is valuable for readability, and can improve performance too (not that Iâve ever had that be a problem, but at least it wonât worsen it).
Maybe {*()} will do, but it feels a bit like a hack to me.