jet
jet copied to clipboard
Inconsistency with add operator
Taking in example
RunJetTest(t, data, nil, "actionNode_AddIntString", `{{ 2+"1" }}`, "3")
RunJetTest(t, data, nil, "actionNode_AddStringInt", `{{ "1"+2 }}`, "12")
The first test the left operand is a number and the right is a string then the right operand is converted to a number and the expression is evaluated as add expression.
The second test the left operand is a string and the right is a number the expression is evaluated as a string concatenation.
I propose a breaking change in the template language, adding a operator for string concatenation and let numeric operators to work only with numbers. Like php $val.$val2.
@annismckenzie
Yes, I concur with this. That means bumping the version to 3.0.
#93 fixes this another way by treating both as arithmetic expressions:
RunJetTest(t, data, nil, "actionNode_AddIntString", `{{ 2+"1" }}`, "3")
RunJetTest(t, data, nil, "actionNode_AddStringInt", `{{ "1"+2 }}`, "3")
I'm not sure if that's a good idea though (I'll raise the issue over there too) because isn't the use case of a template engine to allow string concatenation without caring about the individual pieces' type?
As an example:
{{ someVariable := 10 }}
{{ prefix := "The answer is " }}
{{ suffix := ". Some more text." }}
{{ prefix + someVariable + suffix }}
I'd really expect this to print The answer is 10. Some more text.
. /cc @maxime1907
This is also related to #83.
FYI: the twig/jinja etc use ~ as a concat operator. I don't know if this is reserved here, but It's more readable than the dot as dot can easily be lost in the visual clutter.
So if there will be a different concat oeprator, then the examples above should succedd,
however RunJetTest(t, data, nil, "actionNode_AddIntString",
{{ 2+"1z" }}, "3")
should fail!
@mvrhov I agree with adding a special operator for specific use case like "2" ~ "2" output: "22", but ~ operator is mostly used for regex and binary operations, so it may be confusing ^^ Regarding the changes on the additive function, i made it works that way (same as perl template engine):
- {{ "2" + "2" }} will output: 4
- {{ "2" + "2hey" }} will output: 22hey
- {{ "2" + 2 }} will output: 4
- {{ someVariable := 10 }} {{ prefix := "The answer is " }} {{ suffix := ". Some more text." }} {{ prefix + someVariable + suffix }} will output: The answer is 10. Some more text.
I'll add my examples from above as test cases and then I think I'm coming around to favor this approach. It'll still bump the version to v3 because of the potential breakages.