-
Notifications
You must be signed in to change notification settings - Fork 1.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Is concatenation going to be written +
or something else?
#457
Comments
Even if there were no types that support both addition and concatenation, using distinct operators for addition versus concatenation seems reasonable for expressivity purposes. And I think concatenation is an operation that's common enough that allocating an extra symbol for it would be reasonable; I don't consider allocating an extra symbol to be a significant disadvantage. However, it does seem to create a potential interoperability friction with C++. When calling Carbon from C++, would we map a C++ If we are able to support a single operator token being all of prefix, infix, and postfix, we could use an infix
I think there's a broader question here: do we want to have some notion of semantics associated with our (overloadable) operators, or is it just a free-for-all? For example, should we be able to assume that I think we need to be careful here, and consider how we would fit floating-point types and integer overflow into such a model. Commutativity of |
This already assumes that these binary operators are homogeneous |
I think that the canonical syntax for concatenating N strings in Carbon should have optimal performance in at least the following senses:
It's far from clear that an infix binary operator will be able to satisfy these requirements, regardless of how we spell it; as far as I can tell, the only way to do so is to for the language to provide some way of transforming a chain of operators into the equivalent of a single function call. That could take the form of something like expression templates, or some sort of bespoke language-level rewrite rule as discussed in #451, or maybe some kind of static reflection, or who knows what else. On the other hand, I am very confident that a function call syntax will be able to satisfy these requirements in Carbon, because it can already do so pretty straightforwardly in C++ (see |
I think it's worth covering cross-language precedent a bit more if breaking away from C++ syntax, particularly for string concatenation. Going through the top 20 at https://pypl.github.io/PYPL.html, comparing with https://rosettacode.org/wiki/String_concatenation:
|
There are a collection of operations in this space, including at least these:
We presumably want some combination of these operations to be available, but not necessarily all of them. What use cases do we want to address with concatenation in particular rather than one of the other operations? Given @geoffromer's comment and Carbon's efficiency goal, we should consider eschewing concatenation in favor of other options. |
I suggest calling this "binary concatenation"; in my experience the term "concatenation" is often applied to APIs taking an arbitrary number of operands (e.g.
I think that's a generalization of binary concatenation rather than a special case, for two reasons. The boring reason is that "an arbitrary number" is a generalization rather than special case of "two", but the interesting reason is the addition of "format them in a canonical way": as defined, this operation can take any operand that has a canonical format-as-string operation, whereas concatenation was defined to take only string operands. Interpolation and streaming can likewise be generalized to support arbitrary string-formattable types (e.g. I should note that my use of |
This is commonplace in dynamically-typed languages -- for example, JavaScript and Perl both do this. VBScript does too, but uses |
We triage inactive PRs and issues in order to make it easier to find active work. If this issue should remain active or becomes active again, please comment or remove the |
Context: https://en.m.wikipedia.org/wiki/Level_of_measurement I think the examples you bring up, along with others (timestamps; temperatures in °C or °F), all fit vaguely under the "interval scales" category -- they're references with no magnitude. For an interval type T and a cprresponding offset type O, we need to support the following options:
Hypothetically, we could have different spellings for these operations, but it's hard to imagine this being a problem in practice. The types are different. (I might even suggest that we use different interfaces to represent these operations, even if they map to the same operator tokens.) Concatenation is a very different case for me. Rather than defining new operations, it reuses |
(As an aside, i think vector concatenation may be a better motivating example than string concatenation.) |
Let me add that in Julia, the string concatenation is performed using |
Fundamentally, I think Carbon should consider the Whether it makes sense to use that language concept of addition for a type to mean concatenation is a question for the author of that type. I can imagine types which have really good reasons that the only possible and useful model of addition on the type is concatenation. But I can also imagine many, many types for which that is not the case. @lexi-nadia gives a great example of vectors. I don't think we can hope for Carbon to have the language indicate one way or another here, at least not at this stage. This in turn raises a few tightly related questions we need to answer to close this out: First: should we add a new operator symbol to Carbon so that it could be a language-level symbol for concatenation? I suggest that we do not do this (for now). I think there are a lot of healthy ideas for how to make even types where concatenation is unambiguously not well aliased to addition reasonably ergonomic. We can revisit this in the future if we get substantial information indicating that many types would benefit from this expansion of our operator set. But operators are a reasonably expensive syntactic space to begin with, and I think at least today I don't see nearly enough motivation. I would much prefer to invest in other syntax tools that address the same or similar use cases. Second: should strings in Carbon use addition to mean concatenation? I somewhat strongly think this is not the right direction. There are many challenges with this model raised in this thread already. I'll add one more that for me is particularly important: strings are especially common to be accidentally used instead of some other type. There is even the joking phrase "stringly typed APIs" because strings get (over)used so often when there is notionally some other typed data serialized within the string. Because of this frequent type confusion, I think we should be especially careful in using expression syntaxes with strings that might be surprising to apply to a string. I worry that indeed, they will, and the code will become harder to read as the reader reasonably assumes the wrong type rather than deducing an unconventional operation. Combining this with the other issues, I think we should focus on techniques like string interpolation, APIs like Aside: I'd prefer to not anchor this around commutativity FWIW. I think that we should start without assuming commutativity for operators, even where it is extremely common. For example, if we assume |
This comment was marked as off-topic.
This comment was marked as off-topic.
Leads decision: we follow @chandlerc's most recent comment. |
For example, will you concatenate strings with
+
or another operator? Context: https://en.wikipedia.org/wiki/ConcatenationAdvantage of
+
:Advantage of something else:
+
is unnatural mathematically.The text was updated successfully, but these errors were encountered: