Created
August 21, 2022 11:30
-
-
Save dvt32/ced97bcc3b120feafc4c2205d31156e9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// We can add new functionality to a class without inheriting it | |
fun String.removeFirstLastChar() = this.substring(1, this.length-1) | |
class MyCoolClass { | |
fun myMethod() { | |
println( "hello".removeFirstLastChar() ) | |
println( "505".toLong() ) // Kotlin itself also gives us some extension functions | |
} | |
/* | |
Also very useful for mapping DTOs to entities and vice-versa. Something like: | |
fun UserDto.toEntity() = User(firstName = firstName, lastName = lastName) | |
fun User.toDto() = UserDto(firstName = firstName, lastName = lastName) | |
*/ | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment