vert-x3 / issues

Apache License 2.0
36 stars 7 forks source link

Add asBoolean() to AsyncResult make use in Groovy more consise #576

Closed injecteer closed 3 years ago

injecteer commented 3 years ago

Now to check the AsyncResult instances for success the following code is used:

if( asyncRes.succeeded() ){
  println asyncRes.result()
}else{
  log.error 'Error!', asyncRes.cause()
}

I would be good to add a default method to AsyncResult interface to mirror succeeded():

default boolean asBoolean() {
  return succeeded();
} 

according to Groovy Truth to make Groovy code look simpler to read:

if( asyncRes ){
  println asyncRes.result()
}else{
  log.error 'Error!', asyncRes.cause()
}
vietj commented 3 years ago

can that be done with an extension method in groovy ?

On Thu, Feb 18, 2021 at 1:33 PM injecteer notifications@github.com wrote:

Now to check the AsyncResult instances for success the following code is used:

if( asyncRes.succeeded() ){ println asyncRes.result() }else{ log.error 'Error!', asyncRes.cause() }

I would be good to add a default method to AsyncResult interface to mirror succeeded():

default boolean asBoolean() { return succeeded(); }

to make Groovy code look simpler to read:

if( asyncRes ){ println asyncRes.result() }else{ log.error 'Error!', asyncRes.cause() }

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/vert-x3/issues/issues/576, or unsubscribe https://github.com/notifications/unsubscribe-auth/AABXDCSGM5IDQ5W7V232NBDS7UCJ5ANCNFSM4X2G4HXA .

injecteer commented 3 years ago

You mean with global AST transformation? Yes, sure

The amount of code is going to be relatively big comparing to default interface method though

vietj commented 3 years ago

I think the best would be to cnotribute this as an extension method in vertx-lang-groovy in Vert.x 4

we already have extension methods there for Groovy:

https://github.com/vert-x3/vertx-lang-groovy/blob/master/src/main/java/io/vertx/lang/groovy/VertxExtensionModule.java

On Thu, Feb 18, 2021 at 2:46 PM injecteer notifications@github.com wrote:

You mean with global AST transformation? Yes, sure

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/vert-x3/issues/issues/576#issuecomment-781354060, or unsubscribe https://github.com/notifications/unsubscribe-auth/AABXDCQHOJUN42OABLRBETLS7UK2DANCNFSM4X2G4HXA .

injecteer commented 3 years ago

https://gist.github.com/injecteer/945af9faf519f65a4801a13f2cad2d93

injecteer commented 3 years ago

I tried shortly the method in Gist and it works like a charm

vietj commented 3 years ago

Do you mind doing a PR ?

On Fri, Feb 19, 2021 at 12:41 AM injecteer notifications@github.com wrote:

I tried shortly the method in Gist and it works like a charm

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/vert-x3/issues/issues/576#issuecomment-781707755, or unsubscribe https://github.com/notifications/unsubscribe-auth/AABXDCTM6IUP6RGBCW27MR3S7WQSNANCNFSM4X2G4HXA .

injecteer commented 3 years ago

Have a look please.