It's more an informational piece so you get a deeper understanding of how Kotlin, its type system and the compiler works :) Not really something you can directly implement in your project
@@PhilippLacknerof course you can, let's say you've got multiple when expressions (exhaustive), and you throw exceptions for some of the branches, because these values aren't supported. but those exceptions are identical, or very similar to eachother. so you can just encapsulate it in a function like "throwUnsupported", to reduce repetition. and it will return Nothing just like in your example. that's a real-life use case from literally like a week ago in my project.
To be able to return nothing when any other type is expected. It doesn't make sense to store them in a variable, but it does make sense to return it from a function, the single-expression function.
We MUST implement all the methods of the interface. Interface is a CONTRACT between classes, it 'binds' them to implement all the methods if they come into a 'binding' with the interface.
@@billanderson5727 I watch a tutorial on how to create game in android studio..but a function used was deprecated.. And now android says I need to implement a function from an interface.. But I only want to use only one function..inside there there are many functions that needs to be implemented from Android development documentation
You just provide a default empty implementation by adding curly braces to the function. If you need to use the function from Java, annotate it with @JvmDefault as well.
I always wanted to store a break statement's return value. Now I can finally do it.
lmao
Thanks Philip for another informative quick byte. Rocking as usual.
Just a small doubt... when and how do we use this ? As in like an use case
It's more an informational piece so you get a deeper understanding of how Kotlin, its type system and the compiler works :) Not really something you can directly implement in your project
@@PhilippLacknerof course you can, let's say you've got multiple when expressions (exhaustive), and you throw exceptions for some of the branches, because these values aren't supported. but those exceptions are identical, or very similar to eachother. so you can just encapsulate it in a function like "throwUnsupported", to reduce repetition. and it will return Nothing just like in your example. that's a real-life use case from literally like a week ago in my project.
This tip couldn't have come at a better time. Thanks phil
The goal for me is to write readable code
"break" or "return" aren't readable enough for you?
Boundless capabilities of Kotlin 💪
Ok great, but in what way does this feature represents an advancement in relation to Java? This question comes purely out of curiosity.
Any useful example of this technique?
Like TODO() function
Thank you!🔥
I am a beginner and yr tips are amazing 🎉
Hey philip what's your vs code theme
Im curios, will u ever do iOS/Swift development content?
Y tho
Ok. But why
To be able to return nothing when any other type is expected.
It doesn't make sense to store them in a variable, but it does make sense to return it from a function, the single-expression function.
Generics
Make it infix and and it'll appear as keyword
TODO()
👌👌
How can we implement only one function from an interface if that interface has many functions that needs to be overriden?
We MUST implement all the methods of the interface. Interface is a CONTRACT between classes, it 'binds' them to implement all the methods if they come into a 'binding' with the interface.
@@billanderson5727 I watch a tutorial on how to create game in android studio..but a function used was deprecated..
And now android says I need to implement a function from an interface..
But I only want to use only one function..inside there there are many functions that needs to be implemented from Android development documentation
@@billanderson5727 besides I don't know what code should I write
You just provide a default empty implementation by adding curly braces to the function. If you need to use the function from Java, annotate it with @JvmDefault as well.
If you don't have access to the original interface, you can extend it
Is this similar to Null statment?
nope, nothing is final class in class hierarchy
🙃
I'm that 1% 🦾
I'm not in the 1% 🫤