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

Support for variables #89

Open
adrianhurt opened this issue Dec 8, 2015 · 3 comments
Open

Support for variables #89

adrianhurt opened this issue Dec 8, 2015 · 3 comments

Comments

@adrianhurt
Copy link

I've seen there was another issue for this (#63) but it's closed and I think it's an important lack.

For now, if we need to declare some variables we have to to this:

@defining(
  myObject.calcFoo,
  myObject.calcBar
  ) { case (foo, bar) =>
 @defining(myObject.calcResult(foo, bar)) { result =>

  … the whole template …

  }
}

And it's not handy. However, we can declare reusable blocks:

@foo = @{myObject.calcFoo}
@bar = @{myObject.calcBar}
@result = @{myObject.calcResult(foo, bar)}

But they are treated as functions, so foo, bar and result will be recalculated every time. I think every time a reusable block is declared without parenthesis, its value should be calculated once and use it as a variable. So the previous example would define variables, and the following one would define functions.

@foo() = @{myObject.calcFoo}
@bar() = @{myObject.calcBar}
@result() = @{myObject.calcResult(foo, bar)}
@wsargent
Copy link
Member

@adrianhurt I like the idea in general, but how would this effect already written templates?

@adrianhurt
Copy link
Author

Yeah, I know… But it wouldn't be the first time the backward compatibility is broken. I think it's more logic. Anyway, in my opinion it would be nice to add a note in Play's doc warning that @foo = @{…} is a function. In my case I realized it months later that I was recalculating several time the same value.

There would be more options to write the same thing if the problem is to maintain the backward compatibility:

@foo = (myObject.callcFoo)      // Html
@foo = @(myObject.callcFoo)     // Scala value
@foo = {{myObject.callcFoo}}    // Html
@foo = @{{myObject.callcFoo}}   // Scala value
...

@gmethvin
Copy link
Member

I assume you mean we should support defining immutable values, but not mutable variables.

I believe it's typical scala style to omit parens only for pure functions that are side-effect free. This is what I would typically do at least. So if users are doing that here, @adrianhurt's suggestion should not cause too many backwards compatibility issues. I'm not sure what most code in the wild looks like though.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: 🆕 New
Development

No branches or pull requests

4 participants