{"action":"created","issue":{"url":"https://api.github.com/repos/fizzed/rocker/issues/41","repository_url":"https://api.github.com/repos/fizzed/rocker","labels_url":"https://api.github.com/repos/fizzed/rocker/issues/41/labels{/name}","comments_url":"https://api.github.com/repos/fizzed/rocker/issues/41/comments","events_url":"https://api.github.com/repos/fizzed/rocker/issues/41/events","html_url":"https://github.com/fizzed/rocker/issues/41","id":191242125,"number":41,"title":"Render final keyword for @for loops in java source or source may not compile","user":{"login":"mreuvers","id":10057956,"avatar_url":"https://avatars.githubusercontent.com/u/10057956?v=3","gravatar_id":"","url":"https://api.github.com/users/mreuvers","html_url":"https://github.com/mreuvers","followers_url":"https://api.github.com/users/mreuvers/followers","following_url":"https://api.github.com/users/mreuvers/following{/other_user}","gists_url":"https://api.github.com/users/mreuvers/gists{/gist_id}","starred_url":"https://api.github.com/users/mreuvers/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/mreuvers/subscriptions","organizations_url":"https://api.github.com/users/mreuvers/orgs","repos_url":"https://api.github.com/users/mreuvers/repos","events_url":"https://api.github.com/users/mreuvers/events{/privacy}","received_events_url":"https://api.github.com/users/mreuvers/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":7,"created_at":"2016-11-23T11:05:44Z","updated_at":"2016-11-25T12:09:21Z","closed_at":null,"body":"Hi there,\r\n\r\nI have been using Rocker for our new development project as I accidentally found it and like the concepts. Unfortunately I ran into the following issue while trying it out, see the sample code below.\r\n\r\n`  \r\n          @for (ModelClass modelClass : modelClasses) {\r\n            @with (List<Method> methodsFound = modelClass.getMethods()) {\r\n              @if (!methodsFound.isEmpty()) {\r\n                // Class: @modelClass.getFullyQualifiedName()\r\n              }\r\n            }\r\n          }\r\n`\r\nThe line  Class: @modelClass.getFullyQualifiedName() will not compile, because modelClass is rendered as non-final variable in the template java source (java compiler message: \"Cannot refer to the non-final local variable modelClass defined in an enclosing scope\").\r\n\r\nAfaict the easiest way to deal with this is render these assignments with the final keyword always, that would apply to anything where things are assigned like in the different @for loops (and possibly more?).\r\n\r\nThe above or e.g. class ForStatement needs to deal with an optional 'final' keyword (and probably more different statements).\r\n\r\nI am happy to help you fix this, if you tell me what solution you prefer (I know antlr4 quite well too). But only if you want me to do so. If so, I'll make a pull request then.\r\n\r\nI hope you can help out as it's blocking our progress.\r\n"},"comment":{"url":"https://api.github.com/repos/fizzed/rocker/issues/comments/262943538","html_url":"https://github.com/fizzed/rocker/issues/41#issuecomment-262943538","issue_url":"https://api.github.com/repos/fizzed/rocker/issues/41","id":262943538,"user":{"login":"mreuvers","id":10057956,"avatar_url":"https://avatars.githubusercontent.com/u/10057956?v=3","gravatar_id":"","url":"https://api.github.com/users/mreuvers","html_url":"https://github.com/mreuvers","followers_url":"https://api.github.com/users/mreuvers/followers","following_url":"https://api.github.com/users/mreuvers/following{/other_user}","gists_url":"https://api.github.com/users/mreuvers/gists{/gist_id}","starred_url":"https://api.github.com/users/mreuvers/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/mreuvers/subscriptions","organizations_url":"https://api.github.com/users/mreuvers/orgs","repos_url":"https://api.github.com/users/mreuvers/repos","events_url":"https://api.github.com/users/mreuvers/events{/privacy}","received_events_url":"https://api.github.com/users/mreuvers/received_events","type":"User","site_admin":false},"created_at":"2016-11-25T12:09:21Z","updated_at":"2016-11-25T12:09:21Z","body":"Hey Joe,\r\n\r\nI had a look at the remaining issue. This one is a lot harder to tackle, and we should consider not even trying to fix it (as it's actually how normal java works here). Since we're dealing with ordinary loops like:\r\n\r\n@for(int i = 0; i < x; i++)\r\n\r\nFor one thing 'i' can never be final, since it's part of a loop after all and does change. In theory you could workaround this, by doing the loop in a different fashion and generating it differently, but then you need to fully parse the expression first before you can do that (atm this is stored in ForStatement as 3 parts). Don't think it's really worth the effort.\r\n\r\nWhat I think would make more sense (and that's how you would deal with it in normal java anyway) is to have a separate final assignent variable.\r\n\r\nE.g. like the @with but in a non-scoped way.\r\n\r\nSamples of what I was thinking about:\r\n@assign (String newObject =\tkey.toLowerCase())\r\n@assign (int index = i)\r\nThese assigns will be made final in generation then and are available in the template.\r\n\r\nThis would be a very useful addition, I'd love to have them: specifically to assign things used repeatedly in a template, as well as for expensive methods calls, I rather execute only once than many times.\r\n\r\nThe above will also work for the for loop:\r\n@for (int i = 0; i < x; i++) {\r\n  @assign (int index=i)\r\n  @with (...) {\r\n    @index // Ok\r\n  }\r\n}\r\n\r\nWhat are your thoughts?"}}
{"actor":{"display_login":"mreuvers"}}