How to do this in Kotlin-js?


#1

Js code:

function Ctrl(scope, filter) {   scope.a = filter("abc"); }

I tried to write some Kotlin code like this:

native trait Filter { }

native trait Scope {
  var a:String
}

fun Ctrl(scope:Scope, filter:Filter) {
  scope.a = filter(“abc”)  // !!! how to declare this “()” method in trait Filter?
}

You can see there is a "filter("abc")", it can be understood as "filter.call(this, "abc")". How to declare a proper method for it?


#2

Looks like a function type would help


#3

Do you mean I should declare "Filter" as:

``

fun Ctrl(scope:Scope, filter: (String)->String) {}

But the “filter” may have other methods, e.g. filter.hello()

What should I do for this case?


#4

Define a mwthod named invoke(). See this page: http://confluence.jetbrains.com/display/Kotlin/Operator+overloading