Exception transparency
Reinier Zwitserloot
reinier at zwitserloot.com
Tue Jun 8 10:39:13 PDT 2010
Nathan, I don't think pure means what you think it means. Or I'm horribly
confused, also an option.
"pure" at least in sofar I understand it within the confines of this
discussion, simply means: "Does not assume that the runtime stack matches
the lexical stack". No more, no less. So, everything is pure, unless it is a
closure that does one or more of the following things:
A) Access a variable from lexical scope that is outside the closure itself.
The variable isn't final, and isn't effectively final.
B) returns, breaks, or continues from inside the closure to outside the
closure; i.e. the closure breaks a for loop in the containing method. (Not
on the agenda for java7 closures so far, but nice to make possible, if not
for java7, then for java 8)
C) throws a checked exception which is not handled by the closure (as in,
the closure type doesn't have a "throws TheCheckedException" in its
signature), but it IS handled by the containing method (either because the
closure is defined in a try/catch block that catches that exception, or the
containing method has a "throws TheCheckedException" clause).
Any closure that engages in none of those 3 things is pure. Pure does not
mean "I have no side effects".
--Reinier Zwitserloot
On Tue, Jun 8, 2010 at 5:47 PM, Nathan Bryant
<nathan.bryant at linkshare.com>wrote:
> A simple example:
>
>
>
> public @pure int sum(int a, int b) {
>
> log.debug(“sum!”);
>
> return a + b;
>
> }
>
>
>
> log.debug can never be strictly pure, but surely we don’t want to disallow
> it!
>
>
>
> *From:* Paulo Levi [mailto:i30817 at gmail.com]
> *Sent:* Tuesday, June 08, 2010 11:44 AM
> *To:* Nathan Bryant
> *Cc:* Brian Goetz; Reinier Zwitserloot; lambda-dev at openjdk.java.net
> *Subject:* Re: Exception transparency
>
>
>
> Why is it not workable? Do programmers have trouble understanding that they
> can't use private methods outside of their class to give a foolish example?
>
> Why would they have problems understanding that they can only use "pure"
> methods in the body of a pure function -recursive obvs? (aside, here you can
> see why constructors, and SAM types are still useful, mark a method as pure,
> but leave the constructor to deal with the impurity).
>
> Seems like the most elegant solution, and frankly almost a requirement for
> that the new parallel api's if they are to maintain a little sanity. The
> alternative is to retrofit it later if the concept becomes available, that
> will break code, and thus be used as a excuse to do nothing, as usual.
>
>
More information about the lambda-dev
mailing list