-
Notifications
You must be signed in to change notification settings - Fork 106
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
Regression: Ranging over slice from Fast Function #122
Comments
From the example code and the error message, it's clear that the problem is that Both changing Now as to why this used to work and doesn't work anymore: I can't really tell. The commit that breaks the example changed the slice ranger implementation to pull the actual value out of the Here's a small example of that: https://play.golang.org/p/Y0YvB19a0aH 🤔 |
If I understand jet's lexing and parsing correctly, Line 1089 in 17c3587
|
Yes, that's correct. In a way it is more strict and "correct", but on the other hand Go's normal method semantics allow you to call pointer methods on structs and they magically work, so it seems intuitive that Jet would let you do that. |
(This comment has been redacted because I made a mistake and referenced the wrong branch, so it's inapplicable 😄 ) |
Okay, I believe I get what's happening now. Previously, Now, Thinking about it, this is most definitely desired behavior in terms of the newer ranging style, because when calling Consider if the template looked like this: {{sorted := SortApples(myApples)}}
{{range i, apple := sorted}}
{{apple.SetFlavor("ripe")}}
{{end}}
{{range i, apple := sorted}}
{{i}}: {{apple.GetFlavor()}}
{{end}} You might expect all apples to have a "ripe" flavor but they wouldn't because you're modifying the copy in the first loop. Anyway, I'm fairly certain this is what's happening. @sauerbraten I see in #112 you said:
I get the concept, but do you have some code/template examples of where you ran into this previously? |
What you say about Line 1488 in 17c3587
Apple or *Apple wrapped in an interface, as you can see from the playground example in #122 (comment).
Can you see why that wouldn't happen and instead the |
Two (for us common) use cases regarding my comment in #112 are: checking if a certain string literal is contained in a |
I guess it's because we're dealing with an element of a slice, and the method has a pointer receiver. Check this out: https://play.golang.org/p/suysPI_L6-t It's the most accurate representation of what we're dealing with. You can see that I can't locate the method until I call Does that make sense? |
Would you be able to show a sample for each of these scenarios? Like, for example, is this what you mean by the string comparison: {{range i, n := names}}
{{if n == "Joe"}} <!-- This fails? Really? -->
Hello Joe!
{{else}}
Hello person!
{{end}}
{{end}} On the index: {{index := -1}}
{{range i, n := names}}
{{if CompareString(n, "Joe")}}
{{index = i}}
{{end}}
{{end}} If that's what you mean, I see how that could fail, because it was a pointer to the counter in the sliceRanger: Line 1573 in 2b06453
|
Sorry, I meant checking if a string key was present in a map. The values returned by |
Ahh, I see: |
I'm not sure jet should deviate from the Go spec by using reflection. I'd rather it be as strict about the types you work on as Go is. |
Yeah, precisely :)
Yeah, I do get you, but I think since it's the longstanding behavior of
Interesting, okay. Question on that, because I think a mapRanger is a different type, right? And if we look at the code before your original PR (and now reverting back to that with your new PR) we see: Lines 1620 to 1630 in 62edd43
I don't actually see how it's returning a (I love your new PR and don't want to hold it up, just curious about what you ran into & want to make sure you'll be all good.) |
Hm, yeah I tried out the map keys: https://play.golang.org/p/JWi9QluRV4a Seems fine? Well, I might not have it nailed; if you can throw out an example scenario, we can scope it more. |
Yeah, everything you say makes sense. I'm not sure anymore what our issue was 🤔 I know that a colleague had a problem with comparisons inside a range not working properly before we did #112. I'm not sure anymore why exactly it happened, sadly. Since we're changing it back anyway, I don't think it's super important to recreate it. |
I've observed a regression caused by 6d666f9 (#112)
Summary
For this template:
(Where
SortApples
is a fast function aka jet.Func.)Jet was able to range over the returned slice up until 6d666f9 at which point the template execution would error with:
there is no field or method "GetFlavor" in main.Apple
Example Project
https://github.com/tooolbox/jet-example has two branches:
I understand we're now going for a v3 with these commits, so breaking changes might be expected, but this seems like it would hurt fast functions a lot.
cc @sauerbraten
The text was updated successfully, but these errors were encountered: