I love the way extensions work


#1

 

fun <T> HashSet<T>.add(vararg ts : T): Unit {
  for (t in ts) {
  // apparently this.add() calls the original add function for a HashSet
  this.add(t)
  }
}

This is a beautiful thing.  thanks to the developers.

I just wanted to say thank you!

Eric


#2

Thanks for the kind words.

P.S. you can omit “Unit” in the return type position


#3

I think so, too. Kotlin has a clear plus here. If you look at how extension methods work in Xtend, C#, Ceylon the this pointer always has to be passed on as a method parameter to the extension method. How intuitive is that? @Categories in Groovy are slow since disptach is dynamic and the new extension methods in Groovy 2.0 require you to write a manifest file. Also not very comfortable...