Skip to content
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

Inconsistency with add operator #32

Open
jhsx opened this issue Aug 26, 2016 · 5 comments
Open

Inconsistency with add operator #32

jhsx opened this issue Aug 26, 2016 · 5 comments

Comments

@jhsx
Copy link
Member

jhsx commented Aug 26, 2016

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

@annismckenzie
Copy link
Member

Yes, I concur with this. That means bumping the version to 3.0.

@annismckenzie annismckenzie changed the title Iconsistency with add operator Inconsistency with add operator Sep 17, 2016
@annismckenzie
Copy link
Member

#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.

@mvrhov
Copy link

mvrhov commented Jul 5, 2018

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!

@maxime1907
Copy link

maxime1907 commented Jul 5, 2018

@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.

@annismckenzie
Copy link
Member

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants