[ 
https://issues.apache.org/jira/browse/LUCENE-7796?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=15980152#comment-15980152
 ] 

Uwe Schindler edited comment on LUCENE-7796 at 4/22/17 10:27 PM:
-----------------------------------------------------------------

BTW: I have a better rethrow idiom that does not even need try..catch. Very 
clear code and it clearly shows what happens when used in code. Source:

{code:java}
@FunctionalInterface
public interface Unchecked<R,T extends Throwable> {
  
  R call() throws T;
  
  @SuppressWarnings("unchecked")
  static <R> R exec(Unchecked<R,?> lambda) {
    return ((Unchecked<R,RuntimeException>) lambda).call();
  }
  
}
{code}

Usage:

{code:java}
Something ret = Unchecked.exec(() -> {
  return doCrazyStuff();
});
{code}

This is looking like a try...catch, but it uses a Lambda trick with the same 
sneaky throw trick, although it is very compact and easy to read. Code looks 
very clean and you don't need any compiler hacks like proposed in this issue. 
Only backside: Don't use it in inner loops or when you want to call 
MethodHandles very fast, as it breaks inlining of MethodHandles. So it's a bad 
idea for AttributeFactory or Snowball.


was (Author: thetaphi):
BTW: I have a better rethrow idiom that does not even need try..catch. Very 
clear code and it clearly shows what happens:

{code:java}
@FunctionalInterface
public interface Unchecked<R,T extends Throwable> {
  
  R call() throws T;
  
  @SuppressWarnings("unchecked")
  static <R> R exec(Unchecked<R,?> lambda) {
    return ((Unchecked<R,RuntimeException>) lambda).call();
  }
  
}
{code}

Usage:

{code:java}
Something ret = Unchecked.exec(() -> {
  return doCrazyStuff();
});
{code}

This is looking like a try...catch, but it uses a Lambda trick with the same 
sneaky throw trick, although it is very compact and easy to read. Code looks 
very clean and you don't need any compiler hacks like proposed in this issue. 
Only backside: Don't use it in inner loops or when you want to call 
MethodHandles very fast, as it breaks inlining of MethodHandles. So it's a bad 
idea for AttributeFactory or Snowball.

> Make reThrow idiom declare RuntimeException return type so callers may use it 
> in a way that compiler knows subsequent code is unreachable
> -----------------------------------------------------------------------------------------------------------------------------------------
>
>                 Key: LUCENE-7796
>                 URL: https://issues.apache.org/jira/browse/LUCENE-7796
>             Project: Lucene - Core
>          Issue Type: Improvement
>            Reporter: Dawid Weiss
>            Assignee: Dawid Weiss
>            Priority: Trivial
>             Fix For: 6.x, master (7.0)
>
>
> A spinoff from LUCENE-7792: reThrow can be declared to return an unchecked 
> exception so that callers can choose to use {{throw reThrow(...)}} as an 
> idiom to let the compiler know any subsequent code will be unreachable.



--
This message was sent by Atlassian JIRA
(v6.3.15#6346)

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscr...@lucene.apache.org
For additional commands, e-mail: dev-h...@lucene.apache.org

Reply via email to