https://kotlinlang.org logo
#feed
Title
# feed
s

s1m0nw1

04/08/2019, 8:35 AM
Here's a new article covering inline classes, their inner workings and some use cases https://kotlinexpertise.com/kotlin-inline-classes/
👍 12
m

mkobit

04/08/2019, 1:22 PM
nice article - we've ran into a few issues when trying to use
inline class
• no
init
means you cant can't constrain them on construction (like a
PositiveInt
class where the constructor checks the provided value) • mocking frameworks dont play nice them. if you have a function that takes
fun myFun(positveInt: Int)
and you stub it using something like mockito, then switching it to
fun myFun(positveInt: PositiveInt)
, it is tough to wrangle mockito matchers and verifiers to do what you want them to do
s

s1m0nw1

04/08/2019, 1:24 PM
Thank you - have you tried mockk already?
m

mkobit

04/08/2019, 2:14 PM
have not tried it with mockk yet, but saw (https://github.com/mockk/mockk/issues/152) which looks like it might have some workarounds we could use https://youtrack.jetbrains.com/issue/KT-28055 is for the input constraints which would be nice for us
s

s1m0nw1

04/08/2019, 2:40 PM
I haven’t run into related issues yet but definitely appreciate your heads up!
g

Geert

04/11/2019, 10:15 AM
I am showing ViewModels in a RecyclerView, is it a good usecase to use inline classes with the ViewModel as parameter, and an interface with the method getLayouResId() for specifying the layout resource id? So that the RecyclerViewAdapter automatically can bind the viewmodel with layout to a viewholder?
Hmm, never mind. Cant extend ViewModel
Wait, I think I found a solution for this.
But sinceI use IntViewModel as class, I probably can just add the layoutResId also in there. So no need for inline classes, right?
3 Views