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

Proposal: inline let #7

Closed
taras opened this issue Aug 24, 2016 · 12 comments
Closed

Proposal: inline let #7

taras opened this issue Aug 24, 2016 · 12 comments

Comments

@taras
Copy link
Member

taras commented Aug 24, 2016

I had an idea recently of how we could create an inline version of let helper that'd automatically update a variable when calculation changes.

For example,

{{let hello "world"}}
{{hello}}

Would output "world".

This value would also be reactive, so it'll automatically update the variable binding when calculation changes.

{{let greeting (concat "hello " to)}}

{{greeting}}

<button {{action (mut to) "world"}}>To world</button>

Would output "hello " first, on click of button would set to to "world" and render "hello world".

I can be useful to define variables that apply to the entire template without introducing nesting and to define reactive variables that change based on computation.

@cowboyd
Copy link
Member

cowboyd commented Aug 25, 2016

I'm curious about scoping. What happens if I say:

{{let num 0}}

num = {{num}}

{{#each (list 1 2 3) as |num|}}
  num = {{num}}
{{/each}}

num = {{num}}

I'd expect to see:

num = 0
num = 1
num = 2
num = 3
num = 0

Would it also work with explicit let's in block scope?

{{let num 0}}

num = {{num}}

{{#each (list 1 2 3) as |int|}}
  {{let num int}}
  num = {{num}}
{{/each}}

num = {{num}}

@taras
Copy link
Member Author

taras commented Aug 25, 2016

It does not do anything to resolve the path. It will only effect the path that's passed to it.

This will be a good thing to verify with tests. I'll add these to confirm my assumptions.

@cowboyd
Copy link
Member

cowboyd commented Aug 25, 2016

Any thoughts about multiple binding expressions as per let in most LISP implementations?

http://docs.racket-lang.org/reference/let.html#(form._((lib._racket/private/letstx-scheme..rkt)._let))

{{let height (subtract top bottom) width (subtract right left)}}

@cowboyd
Copy link
Member

cowboyd commented Aug 25, 2016

This is probably a better link for binding in Scheme http://docs.racket-lang.org/guide/let.html

@taras
Copy link
Member Author

taras commented Aug 25, 2016

Multiple binding expressions seem like a great idea and should be easy to implement.

Unfortunately, when one value changes, all of the binding vales will be reapplied but that might not be a problem considering that glimmer is smart about caching the updates.

@taras
Copy link
Member Author

taras commented Aug 25, 2016

I'm curious about scoping.

The scoping test was successful. Got exactly what you(and I) expected.

Would it also work with explicit let's in block scope?

No, this actually seems to cause an infinite loop. Ember throws a warning that {{num}} was modified multiple times in single run loop.

@taras
Copy link
Member Author

taras commented Aug 25, 2016

@cowboyd just added multiple declarations support

@taras
Copy link
Member Author

taras commented Aug 26, 2016

I need to provide feedback in case the user messed up the order and specified value where path should be.

@cowboyd
Copy link
Member

cowboyd commented Aug 26, 2016

One question: for multiple binding assignments, are previous binding expressions visible in subsequent ones? E.g.

{{#let height (subtract top bottom) width (subtract right left) area (multiply height width)}}

Not sure if it should or shouldn't be, or even what's possible, but I think it's important to ascertain which one it is.

@taras
Copy link
Member Author

taras commented Aug 26, 2016

@cowboyd it does allow to be used in consequent assignments. I have a test for this scenario

@taras
Copy link
Member Author

taras commented Aug 26, 2016

I haven't tried anything as collect as your example but I can try in an integration test

@taras
Copy link
Member Author

taras commented Aug 26, 2016

*complex

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

2 participants