String concatenation feature or bug?

Found this the usual way (meaning the hard way):

Background: 2024-10-13,06:25:21 beginLP _nextPos 0

		l_pos = '_' + _nextPos;
		_util.logTime("requestLogPos " + _nextPos + ", l_pos " + l_pos);
		
Background: 2024-10-13,06:25:21 requestLogPos 0, l_pos _

		l_pos = "_" + _nextPos;
		_util.logTime("requestLogPos " + _nextPos + ", l_pos " + l_pos);

Background: 2024-10-13,06:25:21 requestLogPos 0, l_pos _0

Characters can work, as in this:

	public function httpURL(php, rest) {
		return _host + php + '/' + G_uID + '_' + System.getTimer() + '_' + _httpRetrys + '/' + rest;
	}

		_util.logTime("requestLogPos l_pos " + l_pos + ", l_url " + l_url);
	
Background: 2024-10-13,06:25:21 requestLogPos l_pos logPos_0, l_url https://my.example.com/g1/lp/ffa0f28f1a9e91d2133a05fc4ff987c817fb2ab4_361254406_0/logPos_0

Multiple characters OK, single characters bad and have to use string as workaround. Normally it wouldn't matter except that strings are objects which add up after a while.

  • Maybe you're right, and Google and Microsoft are wrong. I've accused them of being wrong about other things as well myself.

    Google and Microsoft aren't taking about Monkey C.

    + (in Monkey C) is a string concatenation operator.

    It doesn't make sense to add the numeric values of two characters.

    So, promoting the chars to strings and then concatenating the strings does something useful.

  • Is this the article you screenshotted? The one which starts off like this?

    https://learn.microsoft.com/en-us/dotnet/visual-basic/programming-guide/language-features/operators-and-expressions/concatenation-operators

    Concatenation operators join multiple strings into a single string. There are two concatenation operators, + and &. Both carry out the basic concatenation operation, as the following example shows.

    "multiple strings"

    Yes, those articles also mention numeric operands, but they're kind of glossing over the fact that any non-string operand has to be converted to string first before concatenation (typically by calling toString()).

    Please show me a language where "+" is offically referred to as the "character/string concatenation operator"? Or the "string/number concatenation operator"? Or the "string/number/boolean/null/undefined/object concatenation operator" (despite the fact that all of types concatenate with "+" when one of the operands is a string?

    By calling "+" the "character/string concatenation operator" you are seeking to justify your belief that adding any other type to a character in Monkey C should result in concatenation. Again, clearly Garmin doesn't see it that way.

    Most people would call "+" the string concatenation operator or simply the concatenation operator, with the understanding that (usually) one of the operands has to be a string for concatenation to work in the first place, and that any non-string operand will be implicitly converted to a string before concatenation.

    Once again, Monkey C has the interesting special case that "+" also concatenates when both operands are characters, but that doesn't constitute an example of a general rule where "+" should always concatenate when one operand is a character.

    Again:

    System.println("A" + false); // prints "Afalse"
    System.println('A' + false); // runtime error

    So clearly Garmin does not agree with you. If Garmin believed that c + x (where c is a char) should result in concatenation in all or most cases, the 2nd line would not be an error.

  • Multiple characters OK, single characters bad and have to use string as workaround. Normally it wouldn't matter except that strings are objects which add up after a while.

    If you have multiple characters together, you have a string. There's no way around that.

  • I've lost track of what y'all are arguing about. I'm moving on.

  • System.println("A " + false); // prints "A false"
    System.println('A ' + false); // runtime error

    So clearly Garmin does not agree with you. If Garmin believed that c + x (where c is a char) should result in concatenation in all or most cases, the 2nd line would not be an error.

    Adding an integer value to a character can be useful to do.

    If it concatenated 'A' + false, then it should also concatenate 'A' + 1 (which means you wouldn’t be able add integers to characters).

    I don't see a problem with how Monkey C works.

  • Like I said, if you think the behavior of adding a Char and Number/Long is unintuitive, you could could file a feature request for the compiler to emit a warning in this case. While I think it's unlikely Garmin would implement that, it's a lot more likely than Garmin changing the behavior to do what you want (which is for adding a Char and a Number to result in string concatenation.)

  • I've lost track of what y'all are arguing about. I'm moving on.

    You were confused from the start. So, no surprise.

  • Adding an integer value to a character can be useful to do.

    If It concatenated 'A' + false, then it should also concatenate 'A' + 1 (which means you can't add integers to characters.

    I don't see a problem with how Monkey C works.

    I agree 100%. My point was that "+" is not a "character/string concatenation operator". Nobody, especially not Garmin, ever said it was.

    (Not only would adding an integer value to a character be useful to do, I would argue it would be part of one of the main reasons for the char type to even exist in Monkey C - i.e. to provide a convenient way to read/write the numerical value of a single character. Note that Char has "only" been around since like CIQ 1.2.0 or so.)

  • I'll also say that if you can actually demonstrate real-world memory savings by using chars in place of strings in certain places, I'm all for it. (I've done much more extreme things in the name of saving a handful of bytes for data fields, especially on old memory-constrained devices.)

    What you can't do is claim that chars should work just like strings (at least in the case of "+") and say that any behavior otherwise is non-deterministic or inconsistent. I mean you can, but you can't expect everyone to automatically agree with you.

    Clearly it's deterministic - Garmin isn't rolling a pair of dice to determine what should happen when you type 'A' + x;

    Saying it's inconsistent is also arguable - inconsistent with what? You can only mean that it's inconsistent with how "+" works for strings, but once again, nobody said that "+" is supposed to work exactly the same for characters and strings, which once again, is why Garmin doesn't call "+" the "character/string concatenation operator". If they did, that might be highly misleading.

    You might also argue it's unintuitive. Well again, that would only depend on your assumption that chars and strings should work the same when it comes to "+". If you could let go of that assumption, you could stop seeing this as a bug or a bad language design.

  • Help me out here. What does the text in the red ellipse in the Monkey C Language Reference say if it does not say:

    "The + operator is also used to concatenate String values."?