Pattern matching in Kotlin


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.


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

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,}            )   }   ) }

Could you not?


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?


Theoretically yes, but I never measured that


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


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


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.


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.


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!


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.


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