Which one should I use between it and this in kotlin when I use extensions class?


Which one should I use between it and this in kotlin when I use extensions class?



ScreenDef is a class, I add a function setDevice for the class, which one is correct between Code A and Code B? why?



I think that Code B is correct, right?



Code C


data class ScreenDef(
val brightness: Int
): DeviceDef


class ScreenHelper(val mContext: Context) {
fun setScreen(aScreenDef: ScreenDef){
}
}



Code A


fun ScreenDef.setDevice(mContext: Context) {
ScreenHelper(mContext).setScreen(this)
}



Code B


fun ScreenDef.setDevice(mContext: Context) {
ScreenHelper(mContext).setScreen(it)
}





Code B cannot even compile?
– Hong Duan
2 days ago





Yes, Code B is wrong!
– HelloCW
2 days ago





If it compiles and works then it's all opinionated. You should stop asking "should" questions.
– JRomero
2 days ago





1 Answer
1



You should use this. it is referred as shorthand if there is only one parameter in lambdas.


this


it


context?.let {
it.resources.getInt(R.int.anyint) // just for example
}



In above snippet, it is the shorthand for lamda parameter(in case of only one parameter).


it


context?.let { cxt -> // here we have manually defined a parameter
cxt.resources.getInt(R.int.anyint) // just for an example
}



In this snippet, instead of it we have created cxt that is exactly same as it.


it


cxt


it



Actually you are taking the concept of Extension function wrong.



You are creating a data class ScreenDef and want to create an extension function to it, why? If you really want to have a member function just create a normal class and have a function in it.


ScreenDef



Extension function should be created when target class is not maintained by you. For example: Activity, Fragments are not maintained by you and if you want to add a custom function, you have to extend them and do it. So to prevent it extension function comes into picture and they are really handy that's why we love it.



You can rather argue, whats wrong with creating extension function for a class created by us. It may or might not be true. It actually depends.



Let's take an example, suppose we have developed a library to draw simple symbols on canvas and there are several function we have created. It turned out to be so good that people are using it, we decided to created advanced version, that can draw more complex symbols that requires using our already developed simple lib. So when we extend the classes of simple lib we might need some functionality to improve some thing etc. in that case if we have imported our simple lib as dependency then its good to create extension function otherwise we would have to create one more child of that class and create desired function. If we have import our lib as source code, we can just go to the source fine and create a function inside it.



I hope it helps.






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Comments

Popular posts from this blog

paramiko-expect timeout is happening after executing the command

Opening a url is failing in Swift

Export result set on Dbeaver to CSV