Search
 
SCRIPT & CODE EXAMPLE
 

JAVA

Kotlin Sealed Classes

class Expr
class Const(val value: Int) : Expr
class Sum(val left: Expr, val right: Expr) : Expr

fun eval(e: Expr): Int =
        when (e) {
            is Const -> e.value
            is Sum -> eval(e.right) + eval(e.left)
            else ->
                throw IllegalArgumentException("Unknown expression")
        }
Comment

sealed class in kotlin example

sealed interface Expr

sealed class MathExpr(): Expr

data class Const(val number: Double) : MathExpr()

data class Sum(val e1: Expr, val e2: Expr) : MathExpr()

object NotANumber : Expr
Comment

sealed class in kotlin example

sealed class Message {
    abstract val messageId: String
}
data class Track(val event: String, override val messageId: String): Message()
Comment

Kotlin Sealed Class

sealed class Expr
class Const(val value: Int) : Expr()
class Sum(val left: Expr, val right: Expr) : Expr()
object NotANumber : Expr()


fun eval(e: Expr): Int =
        when (e) {
            is Const -> e.value
            is Sum -> eval(e.right) + eval(e.left)
            NotANumber -> java.lang.Double.NaN
        }
Comment

PREVIOUS NEXT
Code Example
Java :: bucle for java 
Java :: type casting 
Java :: read properties file outside jar java 
Java :: Write program for problem 1 such that every regex runs as its own thread in java 
Java :: Java Constructor invocations 
Java :: jframe open another frame using button actionlistener 
Java :: java pebble replacestart 
Java :: javafx stage always on top 
Java :: how to write to a txt file in java in the end 
Java :: java writing an object 
Java :: add SOSL to apex Example 
Java :: jdk full form 
Java :: What is the name of the Android function that is used to update the UI (user interface) from a background thread? 
Java :: editable column 
Java :: function compose method java 8 
Java :: java jpanel popup message 
Java :: split string in / java 
Java :: java using the segment Information already before the for-loop 
Java :: how to divide coloumns in a text file into seperate arrays in java 
Java :: string tmp java 
Java :: Java Static Top-level Class 
Java :: TestNG Data Provider example 
Java :: Stringbuilser 
Java :: java gui lookandfeel flatlaf background color 
Java :: save ArrayList into Room Database android studio 
Java :: getcokor from drawable in java android studio 
Java :: how to install java jdk 8 on ubuntu 20.04 for spark 
Java :: 1 2 1 3 2 1 4 3 2 1 3 2 1 2 1 1 java 
Java :: hikari cp oracle jdbc configuration 
Java :: Java’s generic programming does not apply to the primitive types. True or False? 
ADD CONTENT
Topic
Content
Source link
Name
5+8 =