[bug] Concatenate() formula functionality differs from + operator overload

Minimal repro

make a formula (/formula) then enter this:

WithName(Now(), timestamp, Concatenate("Today at ", timestamp.ToTime()))

the result is “Today at 7:59:07 PM”

Contrast this with a similar formula, that uses the overloaded string concatenation functionality of the “+” operator instead of Concatenate():

WithName(Now(), timestamp, "Today at " + timestamp.ToTime())

result is: “Today at 0.8326851851852552”

The difference is just how both operations cast the datetime object to a string. The first gets a nicely formatted output, while the second gets the Time() float value of the object (not sure if that is the internal representation or an actual conversion…)

I would expect Concatenate() to be a more formulaic alternative to the “+” operators, and for both to have the exact same behaviour.

Nobody promised that + would be working well for concatenating strings. Don’t get into habit of using it — use Contatenate() instead.

2 Likes

I’ve been caught by this too. Here are a couple other threads if you want some more details, but you can also just obey Paul’s TL;DR of the situation.

2 Likes

I know it’s not documented, but I don’t see why it would differ.

I can only guess but + can also be used to Sum() some numbers… Coda here might be trying to add timestamp.ToTime() to "Today at " :woman_shrugging: … which returns a mix of some text and a time value…

Yes, it will be two different answers for the reasons stated above. In short, + is a math thing and Concatenate() is a text thing. When dates and times are used in math things, they are converted to a number so that number can then be added to or subtracted from. So it’s not really a broken thing that it happens this way, it’s actually what you’re telling the formula to do.

Definitely stick with what @Paul_Danyliuk said, if you’re looking for text, use Concatenate() or Format() and reserve the + for actual math.

6 Likes

Right. I understand that there is a use case where converting the date or time to a number upon being used as a term in an addition makes sense, I just don’t think it’s this case. Also, the behaviour is more so surprising to me because in most languages the overloading of operators such as the addition operator is contextual (usually by having the added object receive a reference to the thing it is being added to, and choose how to represent itself). So a date could convert itself to a string when added to another string, or to a number when added to another number or date.

In any case this isn’t the documented way of doing things so there is no point putting effort into this path when Concatenate() works fine.

and yet in not so many languages should you really use it.

In Java, the language I’m coming from, we used to explicitly String.concat() or use a StringBuilder (if I remember correctly, pretty much always).

In JavaScript, the language Coda is written in and whose quirks the latter inherits, IIRC string interpolation or working with buffers is now considered the best practices whereas adding anything to anything with + (except math) is discouraged because it can result in all sorts of “wtf js” :slight_smile:

So I’d say leave the + operator rightfully to math.

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.