Why are trait bounds represented by +
?
When there is a trait A
and a trait B
, and the generics argument T
has a trait boundary of A + B
, then T
must be A∩B
, so it might as well be expressed as A * B
.
Why did you use the +
operator?
Old thread:
Why do you ask?
I have a math degree, so I understand what you're getting at. But don't you think someone who doesn't would be confused by a multiplication symbol?
Conceptually, you're adding another requirement to the list of requirements.
Last time @cuviper found the commit.
Multiple type bounds
N: Current syntax uses whitespace as a separator, ambiguous with rooted paths
N: Propose<T:Foo+Bar>
as alternative
N: Graydon?
G: a bit gnarly but tolerable
N: Good enough!
B: We're done here. Thanks
Speaking as a mathematical midget I start thinking:
Were I to be looking for a job I might want a salary S and a bonus B and a C and X weeks of holiday. The are the traits I might be looking for. These things all add up so: A + B + C + X. So use of "+" in requiring multiple traits seems reasonable.
Which reminds me. Ever since school day I have always wondered why logical "and" is "." and logical "or" is "+". Always seemed backwards to me.
Except that in binary and modulo 2:
0 + 0 = 0
0 + 1 = 1
1 + 0 = 1
1 + 1 = 0
So the arithmetic plus, is actually logical "or" and the symbol for both is "+".
Very confusing.
Note that the choice of an "and" or "or" of any kind depends on the context in which you use it. Compare these two ways of computing the same thing:
let has_a = input & A == A;
let has_b = input & B == B;
let has_all = has_a && has_b;
let wanted = A | B;
let has_all = input & wanted == wanted;
In one case we use an "and" to combine the two parts, and in another case, we use an "or".
Where does anyone denote set intersection with A * B
? The characteristic function of the intersection is the product of characteristic functions, but other than that, I can't think of any examples for that notation. If I saw A * B
, I'd think of the cartesian product, which has nothing to do with multiple trait bounds. A + B
also isn't particularly related to set intersection, since it would mean a disjoint sum. But at least +
has the correct informal meaning "both of those things", while *
has no relevant intuitions.
+
is also commonly expected to be a commutative and associative operator, and compound trait bounds certainly behave this way. *
is more often used in mathematics in cases where the order matters, making it a bit more ambiguous.
&
or &&
would probably be the most intuitive connective, but it would introduce parsing ambiguities with reference types. ,
would be another nice connective, in which case generic parameters and where
-bounds would need a different separator. ;
would likely work for that, but it's a bit less intuitive and pretty for writing sequences of parameters.
This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.