# Pattern matching in Kotlin

**URL:** <https://discuss.kotlinlang.org/t/pattern-matching-in-kotlin/598>\
**Category:** Uncategorized\
**Created:** [April 8, 2014, 12:38pm UTC](https://discuss.kotlinlang.org/t/pattern-matching-in-kotlin/598 "2014-04-08T12:38:19Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![kenbarc](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/kenbarc/32/1693_2.png) [@kenbarc](https://discuss.kotlinlang.org/u/kenbarc)\
**Post date:** [April 8, 2014, 12:38pm UTC](https://discuss.kotlinlang.org/t/pattern-matching-in-kotlin/598/1 "2014-04-08T12:38:19Z")

</div>

The past year I have evolved a simple _pattern matching_ scheme that I now automatically incorporate into new Kotlin class hierarchies. The scheme is trivial to include, costs nothing to program and offers some beneficial features. Its development is thanks to the many nice features offered by Kotlin. It is described [here](http://kenbarclay.blogspot.co.uk/2014/04/kotlin-pattern-matching.html).

---

<div class="post-metadata">

**Author:** ![abreslav](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/abreslav/32/8279_2.png) [@abreslav](https://discuss.kotlinlang.org/u/abreslav)\
**Post date:** [April 8, 2014, 12:46pm UTC](https://discuss.kotlinlang.org/t/pattern-matching-in-kotlin/598/2 "2014-04-08T12:46:21Z")

</div>

Looks very nice indeed! I think you could omit the types in match:

```kotlin

override fun interleave(xs: ListIF<A>): ListIF<A> {
    return this.match(
        {Nil<A>()},
        {cons1 ->
           xs.match(
           {Nil<A>()},
           {cons2 -> Cons(cons1.hd, Cons(cons2.hd, cons1.tl.interleave(cons2.tl)))}
           )
        }
    )
}

```

Could you not?

---

<div class="post-metadata">

**Author:** ![kenbarc](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/kenbarc/32/1693_2.png) [@kenbarc](https://discuss.kotlinlang.org/u/kenbarc)\
**Post date:** [April 8, 2014, 4:07pm UTC](https://discuss.kotlinlang.org/t/pattern-matching-in-kotlin/598/3 "2014-04-08T16:07:02Z")

</div>

That is correct. I deliberately left in the types to benefit the reader.

While I have your attention, does the inclusion of explicit types means the compiler has less work to do and so will compile a library quicker?

---

<div class="post-metadata">

**Author:** ![abreslav](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/abreslav/32/8279_2.png) [@abreslav](https://discuss.kotlinlang.org/u/abreslav)\
**Post date:** [April 8, 2014, 4:11pm UTC](https://discuss.kotlinlang.org/t/pattern-matching-in-kotlin/598/4 "2014-04-08T16:11:19Z")

</div>

Theoretically yes, but I never measured that

---

<div class="post-metadata">

**Author:** ![dodyg](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/dodyg/32/1451_2.png) [@dodyg](https://discuss.kotlinlang.org/u/dodyg)\
**Post date:** [April 13, 2014, 8:45am UTC](https://discuss.kotlinlang.org/t/pattern-matching-in-kotlin/598/5 "2014-04-13T08:45:30Z")

</div>

This is sweet. Would you mind to state the license for the code?

---

<div class="post-metadata">

**Author:** ![kenbarc](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/kenbarc/32/1693_2.png) [@kenbarc](https://discuss.kotlinlang.org/u/kenbarc)\
**Post date:** [April 13, 2014, 1:42pm UTC](https://discuss.kotlinlang.org/t/pattern-matching-in-kotlin/598/6 "2014-04-13T13:42:11Z")

</div>

There is no licence associated with any of my Kotlin code. Perhaps simply acknowledge the source if you borrow the pattern.

---

<div class="post-metadata">

**Author:** ![hhariri](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/hhariri/32/1454_2.png) [@hhariri](https://discuss.kotlinlang.org/u/hhariri)\
**Post date:** [April 13, 2014, 1:46pm UTC](https://discuss.kotlinlang.org/t/pattern-matching-in-kotlin/598/7 "2014-04-13T13:46:16Z")

</div>

Maybe then just add a MIT or Apache 2 license to it? Just suggesting it cause a lot of peple won't see this thread and if they come across the code on GitHub might want to use it but won't because there's no license associated to it.

---

<div class="post-metadata">

**Author:** ![Russel](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/russel/32/1442_2.png) [@Russel](https://discuss.kotlinlang.org/u/Russel)\
**Post date:** [April 13, 2014, 5:20pm UTC](https://discuss.kotlinlang.org/t/pattern-matching-in-kotlin/598/8 "2014-04-13T17:20:34Z")

</div>

I am not a lawyer.

Having no licence for code is problematic as this leaves things unknown and jurisdiction dependent. For example in the UK there is potential for no licence to mean “public domain” which would allow someone to take the code, use it for whatever means, and make no reference to the original author. If your intention is to allow any and all uses as long as there is attribution, then choosing one of the very permissive licences such as ASL2, BSD or MIT would be a wise move. This then makes things clear, at least in the majority of jusridictions.

---

<div class="post-metadata">

**Author:** ![kenbarc](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/kenbarc/32/1693_2.png) [@kenbarc](https://discuss.kotlinlang.org/u/kenbarc)\
**Post date:** [April 13, 2014, 6:11pm UTC](https://discuss.kotlinlang.org/t/pattern-matching-in-kotlin/598/9 "2014-04-13T18:11:21Z")

</div>

Oh goodness. What have I done.

My aim was to describe a scheme that might be of value to Kotlin developers.  
The approach evolved with my own class hierarchies.  
I do not even know if it is already in use in other languages.

Next time I will watch my language!

---

<div class="post-metadata">

**Author:** ![dodyg](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/dodyg/32/1451_2.png) [@dodyg](https://discuss.kotlinlang.org/u/dodyg)\
**Post date:** [April 14, 2014, 7:29am UTC](https://discuss.kotlinlang.org/t/pattern-matching-in-kotlin/598/10 "2014-04-14T07:29:41Z")

</div>

I didn't mean to open this can of worm.

A lot of large companies now employ code scanner that scour the web for code repositories and do text comparison with their own source code (similar to plagiarism detector that universities employ). When they flag a piece of code of being available elsewhere on the Internet, usually the the question of license of the code snippets is raised.

---

<div class="post-metadata">

**Author:** ![john](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/john/32/2072_2.png) [@john](https://discuss.kotlinlang.org/u/john)\
**Post date:** [October 26, 2016, 8:15am UTC](https://discuss.kotlinlang.org/t/pattern-matching-in-kotlin/598/11 "2016-10-26T08:15:05Z")

</div>

Would be really nice to have better pattern matching in Kotlin. Any other plans on getting this included?

---

<div class="post-metadata">

**Author:** ![computadorgranpiador](https://avatars.discourse-cdn.com/v4/letter/c/71c47a/32.png) [@computadorgranpiador](https://discuss.kotlinlang.org/u/computadorgranpiador)\
**Post date:** [February 21, 2026, 9:00pm UTC](https://discuss.kotlinlang.org/t/pattern-matching-in-kotlin/598/13 "2026-02-21T21:00:55Z")

</div>

I prefer:

```auto
when (myVar) {
  Client(val name, ActiveMembership(PlatinumMember(var id, val membershipStartDate))) -> incrementDate(id, membershipStartDate)
  val Client(name, ActiveMembership(RegularMembership(id))) -> updateSettings(id)
  Client(val name, Expired(val anyMembershipType)) -> sendWarning(anyMembershipType)
  VipClinet(val name, val membershipStatus) -> freeRenewal(membershipStatus)
}

```
