0

New to Kotlin, I have seen this code:

val myModule : Module = module { viewModel { MyViewModel(get()) } single { MyRepository() } } 

Looking at the Kotlin docs, it isn't clear to me what the braces mean after "module". Is module a function and the braces are used to initialize the function? If this is true, can you point me to the part in the Kotlin documentation that indicates this? I can't find anything in the docs that shows an example of this. Here is the link:

https://kotlinlang.org/docs/reference/properties.html

2 Answers 2

2

Note that your example seems like Koin code.

In a more general sense: In kotlin when the last parameter of a function is another function ( see Higher order functions) you can put it outside the parenthesis, and if it is the only (non optional) parameter you can omit the parenthesis enterily.

In your example module viewModel and single are functions that take another function as their only parameter, this way you can pass the lambda defining this paramter directly without any parenthesis.

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

1 Comment

Coming from a Java background, I easily forget that Kotlin rule about being able to place the last parameter outside the paranthesis if it's a function. You're also right about it being code from Koin. Thanks.
1

The braces mean that the module function receives a lambda as a parameter. http://kotlinlang.org/docs/reference/lambdas.html#passing-a-lambda-to-the-last-parameter

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.