# Expressions in Painless DSL

**URL:** <https://discuss.elastic.co/t/expressions-in-painless-dsl/171502>\
**Category:** Elasticsearch\
**Tags:** painless\
**Created:** [March 8, 2019, 1:37pm UTC](https://discuss.elastic.co/t/expressions-in-painless-dsl/171502 "2019-03-08T13:37:03Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ryan\_Grannell](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ryan_grannell/32/41712_2.png) [@Ryan\_Grannell](https://discuss.elastic.co/u/Ryan_Grannell)\
**Post date:** [March 8, 2019, 1:37pm UTC](https://discuss.elastic.co/t/expressions-in-painless-dsl/171502/1 "2019-03-08T13:37:03Z")

</div>

Hi,

Does Painless support immediately-invoked function expressions (IIFEs)? For reasons (I'd prefer not to explain them) I need to return the result of several expressions to an if-statement, as shown in an example below.

```auto
if ( (function () { def x = false; return !x; })() ) {
  // -- do something.
}

```

I haven't found a way to get IIFE's working in Painless, are they supported?

Just to clarify, I can't write

```auto
boolean someFn () {
  def x = false;
  return !x;
}

def condition = someFn();

if (condition) {
  // -- do something
}

```

in this case, I need to use an IIFE.

Thanks for reading,

---

<div class="post-metadata">

**Author:** ![gbrown](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/gbrown/32/34482_2.png) [@gbrown](https://discuss.elastic.co/u/gbrown)\
**Post date:** [March 8, 2019, 6:33pm UTC](https://discuss.elastic.co/t/expressions-in-painless-dsl/171502/2 "2019-03-08T18:33:14Z")

</div>

I don't believe Painless supports this directly, but you can fake it by creating a method that takes a [`BooleanSupplier`](https://docs.oracle.com/javase/8/docs/api/java/util/function/BooleanSupplier.html) (or whatever other functional interface you need) and just returns the result:

```auto
POST _scripts/painless/_execute
{
  "script": {
    "source": """
    boolean exec(BooleanSupplier sup) {
      return sup.getAsBoolean();
    }
    if(exec(() -> {boolean x = params.val;return !x;})) {
      return "Fake IIFEs!"
    }
    """,
    "params": {
      "val": false
    }
  }
}

```

Which returns:

```auto
{
  "result" : "Fake IIFEs!"
}

```

Does that work for you?

---

<div class="post-metadata">

**Author:** ![Ryan\_Grannell](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ryan_grannell/32/41712_2.png) [@Ryan\_Grannell](https://discuss.elastic.co/u/Ryan_Grannell)\
**Post date:** [March 11, 2019, 4:00pm UTC](https://discuss.elastic.co/t/expressions-in-painless-dsl/171502/3 "2019-03-11T16:00:56Z")

</div>

Thanks, that works perfectly!

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [April 8, 2019, 4:04pm UTC](https://discuss.elastic.co/t/expressions-in-painless-dsl/171502/4 "2019-04-08T16:04:42Z")

</div>

Closed on April 8, 2019, 4:04pm UTC

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
