Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save dvt32/ced97bcc3b120feafc4c2205d31156e9 to your computer and use it in GitHub Desktop.
Save dvt32/ced97bcc3b120feafc4c2205d31156e9 to your computer and use it in GitHub Desktop.
// 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