# Using ! with infix functions that return Boolean

**URL:** <https://discuss.kotlinlang.org/t/using-with-infix-functions-that-return-boolean/24464>\
**Category:** Language Design\
**Created:** [March 22, 2022, 11:15pm UTC](https://discuss.kotlinlang.org/t/using-with-infix-functions-that-return-boolean/24464 "2022-03-22T23:15:15Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![TrevorHackman](https://avatars.discourse-cdn.com/v4/letter/t/f05b48/32.png) [@TrevorHackman](https://discuss.kotlinlang.org/u/TrevorHackman)\
**Post date:** [March 22, 2022, 11:15pm UTC](https://discuss.kotlinlang.org/t/using-with-infix-functions-that-return-boolean/24464/1 "2022-03-22T23:15:15Z")

</div>

Just an idea,

If I write an infix function that returns a boolean, such as

`fun String.matches(pattern: String): Boolean { ... }`

I thought it would be really natural when I want to check the inverse if I could use !, like

`val doesNotMatch = "..." !matches "..."`

Similar to `=` vs `!=`. But sadly, this is impossible. Kotlin forces you to do it outside like, `!("..." matches "...")`

Anyone agree with me that `"..." !matches "..."` would be nice?

---

<div class="post-metadata">

**Author:** ![medium](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/medium/32/10404_2.png) [@medium](https://discuss.kotlinlang.org/u/medium)\
**Post date:** [March 23, 2022, 12:13pm UTC](https://discuss.kotlinlang.org/t/using-with-infix-functions-that-return-boolean/24464/2 "2022-03-23T12:13:55Z")

</div>

I don’t think that this could easily be achieved. The Kotlin standard libraray simply uses `not` as a prefix, so in your case it would be `"..." notMatches "..."`. Not that bad, I think. It is just two additional keystrokes which hardly affect the life-time of your keyboard 😉 And it is very readable.

If I had designed Kotlin, I would have used `and` instead of `&&` for logical expressions and I think words, especially if they are short, are good in code (there is a reason why the written word is important in almost all cultures).

---

<div class="post-metadata">

**Author:** ![TrevorHackman](https://avatars.discourse-cdn.com/v4/letter/t/f05b48/32.png) [@TrevorHackman](https://discuss.kotlinlang.org/u/TrevorHackman)\
**Post date:** [March 23, 2022, 6:08pm UTC](https://discuss.kotlinlang.org/t/using-with-infix-functions-that-return-boolean/24464/3 "2022-03-23T18:08:49Z")

</div>

Sure I agree `notMatches` reads very nicely, but that requires writing a second function called `notMatches`, so then you need to write twice as many functions if you’re making a library full of these infix functions that return boolean.

---

<div class="post-metadata">

**Author:** ![gidds](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/gidds/32/10865_2.png) [@gidds](https://discuss.kotlinlang.org/u/gidds)\
**Post date:** [March 23, 2022, 7:43pm UTC](https://discuss.kotlinlang.org/t/using-with-infix-functions-that-return-boolean/24464/4 "2022-03-23T19:43:09Z")

</div>

An interesting idea!

Of course it would apply only to predicates (functions returning a `Boolean`).

I’m not sure whether it would work well for the usual function-call syntax:

```
a.!predicate(b)

```

But it would seem very natural and logical for [`infix`](https://kotlinlang.org/docs/functions.html#infix-notation) functions\*. The meaning should be obvious to anyone who knows Kotlin (or has used a `!=` operator), and the implementation would simply call the predicate and then invert the result:

```
a !predicate b

```

would be syntactic sugar for:

```
!(a predicate b)

```

From an æsthetic point of view, it would make the language more regular, as the `!is` and `!in` operators would no longer be special cases but work the same way as all other predicates. And by moving the `!` nearer the predicate, it would make code easier to read, closer to English, and reduce the need for complex expressions in parens — and for `notXxx` duplicates of functions.

And I _think_ it would be completely backward-compatible, too, so it wouldn’t break any existing code.

(I’ve seen many suggestions for language changes, and most of them provide less benefit than would justify the extra complexity or breaking changes — certainly not enough to overcome the initial [minus 100 points](https://gist.github.com/stuartd/a100fb315f2e5ca9ed17551d8b538a60). But this one really appeals to me.)

* * *

(\* I haven’t seen an official reason for restricting infix usage to functions explicitly defined for it, but I suspect it’s for clarity: if _every_ function could be used that way, some people might be tempted to do away with `.` and `()`s, making code much harder to parse. Or maybe that avoids some syntactic ambiguities. Can anyone confirm?)

---

<div class="post-metadata">

**Author:** ![nbness1337](https://avatars.discourse-cdn.com/v4/letter/n/73ab20/32.png) [@nbness1337](https://discuss.kotlinlang.org/u/nbness1337)\
**Post date:** [March 23, 2022, 8:57pm UTC](https://discuss.kotlinlang.org/t/using-with-infix-functions-that-return-boolean/24464/5 "2022-03-23T20:57:48Z")

</div>

I was just writing a DSL and thought about this. Came here to suggest it, lo and behold you already did!

I think it would be a good addition, honestly. It matches the idea of many of kotlin’s features! It makes your code feel more natural!

However it should probably only apply to functions that return `Boolean` for obvious reasons.

---

<div class="post-metadata">

**Author:** ![ilya.gorbunov](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/ilya.gorbunov/32/1645_2.png) [@ilya.gorbunov](https://discuss.kotlinlang.org/u/ilya.gorbunov)\
**Post date:** [March 24, 2022, 2:53am UTC](https://discuss.kotlinlang.org/t/using-with-infix-functions-that-return-boolean/24464/6 "2022-03-24T02:53:39Z")

</div>

> [@TrevorHackman](#):
>
> Anyone agree with me that `"..." !matches "..."` would be nice?

There’s a feature request about that, [KT-5351](https://youtrack.jetbrains.com/issue/KT-5351), and at the moment 38 people there including me agree with you 🙂
