Bug 421348 - [1.8][syntax highlighting] Make implicit references from lambda body to outer scope visible
Summary: [1.8][syntax highlighting] Make implicit references from lambda body to outer...
Status: ASSIGNED
Alias: None
Product: JDT
Classification: Eclipse Project
Component: Text (show other bugs)
Version: 4.4   Edit
Hardware: All All
: P3 enhancement (vote)
Target Milestone: ---   Edit
Assignee: JDT-Text-Inbox CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on: 420660
Blocks:
  Show dependency tree
 
Reported: 2013-11-08 11:06 EST by Markus Keller CLA
Modified: 2014-04-10 03:55 EDT (History)
1 user (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Markus Keller CLA 2013-11-08 11:06:08 EST
Lambda bodies can refer to 'this' of an enclosing scope. Such references to 'this' can lead to inadvertent leaks of the outer object.

Anonymous and other inner classes always keep a reference to the outer object, but lambda expressions only do that if necessary (current spec is not clear about this, but beta implementations agree).

The Java editor should allow the user to quickly determine whether a lambda expression makes use of 'this' or not.

A compile warning/error is not the right device, since references to 'this' are perfectly OK -- they should just be visible.

Possible solution: Semantic highlighting. We already render 'static' invocations italic by default, so we could also render a 'static' lambda expression italic.

Problem is that an italic '->' is barely distinguishable from an upright '->'. Also rendering '()' and '{}'/';' italic could help a bit. We can't apply the style to the whole lambda, since that would override too many other styles (same dilemma as for boxing/unboxing).

Maybe we could show a hover on the '->' that tells whether the lambda binds 'this' or not. The hover could also list the referenced final and effectively final local variables.
Comment 1 Timo Kinnunen CLA 2014-04-10 03:55:31 EDT
(In reply to comment #0)
> We can't apply the
> style to the whole lambda, since that would override too many other styles (same
> dilemma as for boxing/unboxing).

Well, you could add CSS classes like class='static-lambda lambda' or class='nested-lambda lambda' to the whole expression and then query the CSS Engine for how to style each sub-part and its combination of CSS classes. The CSS engine could then make everything having 'nested-lambda' underlined, for example.

Well, you could if you had this technology. It seems that what's going to happen instead is that the CSS Engine will be relying on you and your preference store keys for its "CSS classes" it will be styling, so to get the behavior from the afore-mentioned example you'd have to create "nested-lambda"-versions of all of your preference store keys and provide them with values, and somehow surface this in the UI. See bug 422536 for a patch in progress.