![]() Since Kotlin 1.3 the "main" function can also be defined without any parameters. The function is passed an array containing any command-line arguments. package /* The entry point to a Kotlin program is a function named "main". */ // The "package" keyword works in the same way as in Java. In both cases the scope is changed so that if you declare a local variable within that scope it will not be visible from outer scope, and in 1 case the context changes and in the other one it remains the same and hence the usage of "this" and "it".// Single-line comments start with // /* Multi-line comments look like this. The context and the scope are 2 different things. It.city = "London" //you can not skip writing "it" because you are in outer context It.age = 20 //"it" refers to the object's reference holder e.g. As mentioned above, it is used to delegate the implementation of a property. Same outer context in function's inner scope Uses of the by Keyword in Kotlin The by keyword is helpful in two places: (i) delegating the implementation of interface and (ii) accessors of a property to another object. this functional method parameter can give you integer at the time of invocation.Ĭheck out the snippet below: fun someMethodWithCallback(callback: (Int) -> Unit) it DOES NOT change the context of function's scope, so it remains the same as the context where the function was called, e.g.: //Outer context in outer scope Whatever written above simply means: (Int) -> Unit. Parameter by callback means you want to give your callback a parameter that caller can use on the time of invocation, also considered as it.So, there are two possibilities how you want your callback to be: Let's say you've written a function or using a function which provides you callback as lambda method receiver. ![]() Difference between it & this keywords can be explained by taking example of lambda method receivers (a.k.a higher order functions). ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |