Last active
October 10, 2021 18:10
-
-
Save ghasemdev/0858dedb9f95eb10d2ab6656694d7d56 to your computer and use it in GitHub Desktop.
Advanced Kotlin (DataClass, Delegation, InlineClass, InnerClass, Object, SealedClass, TypeAlias)
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
fun main(args: Array<String>) { | |
val reza = Student("reza", 22) | |
val ghasem = Student("ghasem", 22) | |
println(ghasem.hashCode()) | |
} | |
/*class Student(val name: String, private val age: Int) { | |
fun printPlan() = "Plan" | |
override fun toString(): String { | |
return "(name=$name, age=$age)" | |
} | |
override fun hashCode(): Int { | |
return toString().hashCode() | |
} | |
override fun equals(other: Any?): Boolean { | |
return this.age == (other as Student).age | |
} | |
}*/ | |
data class Student(val name: String = "", var age: Int = 0) { | |
fun foo() = "" | |
} |
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
interface CoffeeMachine { | |
fun makeSmallCoffee() | |
fun makeLargeCoffee() | |
} | |
class NormalCoffeeMachine : CoffeeMachine { | |
override fun makeSmallCoffee() = println("Normal: Making small coffee") | |
override fun makeLargeCoffee() = println("Normal: Making large coffee") | |
} | |
//Decorator: | |
class EnhancedCoffeeMachine(val coffeeMachine: CoffeeMachine) : CoffeeMachine by coffeeMachine { | |
// overriding behaviour | |
override fun makeLargeCoffee() { | |
println("Enhanced: Making large coffee") | |
coffeeMachine.makeLargeCoffee() | |
} | |
// extended behaviour | |
fun makeCoffeeWithMilk() { | |
println("Enhanced: Making coffee with milk") | |
coffeeMachine.makeSmallCoffee() | |
println("Enhanced: Adding milk") | |
} | |
} | |
fun main() { | |
val normalCoffeeMachine = NormalCoffeeMachine() | |
normalCoffeeMachine.makeSmallCoffee() | |
normalCoffeeMachine.makeLargeCoffee() | |
val enhancedCoffeeMachine = EnhancedCoffeeMachine(normalCoffeeMachine) | |
enhancedCoffeeMachine.makeSmallCoffee() | |
enhancedCoffeeMachine.makeLargeCoffee() | |
enhancedCoffeeMachine.makeCoffeeWithMilk() | |
} |
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
import kotlin.time.Duration | |
import kotlin.time.ExperimentalTime | |
interface Validation { | |
fun isValid(): Boolean | |
} | |
@JvmInline | |
value class Email(val value: String) : Validation { | |
override fun isValid(): Boolean = value.matches(Regex("""^[a-zA-Z0-9_!#$%&’*+/=?`{|}~^.-]+@[a-zA-Z0-9.-]+$""")) | |
} | |
@JvmInline | |
value class Phone(val value: String) : Validation { | |
override fun isValid() = value.matches(Regex("""09\d{9}|\+989\d{9}""")) | |
} | |
@OptIn(ExperimentalTime::class) | |
fun main() { | |
val email = Email("ghasem79.dev@gmail.com") | |
println("${email.value} ${email.isValid()}") | |
val phone = Phone("09150000000") | |
println("${phone.value} ${phone.isValid()}") | |
delayTime(Duration.hours(2.5)) | |
} | |
@OptIn(ExperimentalTime::class) | |
fun delayTime(value: Duration) { | |
println(value.inWholeMinutes) | |
} |
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
class Shape { | |
private val color = "red" | |
inner class Circle { | |
fun foo() = println("Circle: $color") | |
} | |
class Rectangle { | |
fun foo() = println("Rectangle") | |
} | |
} | |
fun main() { | |
Shape().Circle().foo() | |
Shape.Rectangle().foo() | |
} |
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
object Database { | |
private val list = mutableListOf<String>() | |
init { | |
println("init") | |
} | |
fun save(value: String) { | |
list.add(value) | |
} | |
fun sout() { | |
list.forEach(::println) | |
} | |
} | |
fun main() { | |
Database.save("ali") | |
Database.save("javad") | |
Database.save("jakode") | |
Database.sout() | |
} |
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
enum class State(val value: Int) { | |
Open(0), Closed(1) | |
} | |
data class Door(var state: State = State.Closed) | |
sealed interface Result<T> | |
object Loading : Result<Nothing> | |
data class Success<T>(val value: T) : Result<T> | |
data class Error(val exception: Exception) : Result<Nothing> | |
fun main() { | |
val door = Door() | |
println(door.state.value) | |
val result: Result<Nothing> = Loading | |
when (result) { | |
is Loading -> { | |
println("loading") | |
} | |
is Success -> { | |
println("Success : ${result.value}") | |
} | |
is Error -> { | |
println("Error : ${result.exception.message}") | |
} | |
} | |
} |
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
typealias Location = Pair<Double, Double> | |
fun main() { | |
val location = Location(12.256, 13.265) | |
println(location) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment