2

I want to pass one or more function(s) to a function but as long as it is a function literal or a package function it's working but when I change it to a function of a specific class (member function) I have the following problems

suppose I have these two functions

fun foo() { //doSomething }
fun bar(function: () -> Unit) { 
    //anotherThing! 
    function()
}

when I call

bar(foo()) 

or

bar(::foo)

I've encountered a type mismatch (Required: ()->Unit , Found: Unit)

Note: I don't want to solve it like this

bar( { foo() } )

or

bar {
foo()
}
13
  • 1
    This feature is not implemented yet, see stackoverflow.com/questions/28022388/… Commented Nov 23, 2015 at 15:50
  • It works for me -- try.kotlinlang.org/#/UserProjects/f9jhaiqob1emh7ho02m7embdsd/… Commented Nov 23, 2015 at 15:55
  • is it foo member fun? Commented Nov 23, 2015 at 15:56
  • yes it's a member function (I know which it's working for package functions) Commented Nov 23, 2015 at 16:00
  • Well, let's fix question. Commented Nov 24, 2015 at 16:09

2 Answers 2

1

In Kotlin 1.1 and later you can use bound callable references. e.g. bar(::foo).

In the second 1.0 Beta Candidate and other prior versions you could not pass around member functions. This issue was tracked as KT-6947.

Sign up to request clarification or add additional context in comments.

Comments

0

I have just checked your example. It's works for me. Kotlin version 1.0.0-beta-2423

Code in Idea

1 Comment

I forgot to say which the function that I want to pass can be a member function otherwise you're right

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.