tyrion

kotlin interface equals

Oct 8th, 2017
254
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 0.75 KB | None | 0 0
  1. import kotlin.test.assertEquals
  2. import kotlin.test.assertNotEquals
  3.  
  4. interface Token<T: Enum<T>> {
  5.     val type: T
  6.     val value: String
  7.         get() = type.name.toLowerCase()
  8. }
  9.  
  10. enum class ExampleToken: Token<ExampleToken> {
  11.     A,
  12.     B,
  13.     C;
  14.  
  15.     override val type = this
  16. }
  17. class ConcreteToken<T: Enum<T>>(override val type: T, override val value: String): Token<T>
  18.  
  19.  
  20. fun main(args: Array<String>) {
  21.     val t1: Token<ExampleToken> = ExampleToken.A
  22.     val t2: Token<ExampleToken> = ExampleToken.B
  23.     val t3: Token<ExampleToken> = ConcreteToken(ExampleToken.A, "nope")
  24.     val t4: Token<ExampleToken> = ConcreteToken(ExampleToken.A, "a")
  25.  
  26.     assertNotEquals(t1, t2)
  27.     assertNotEquals(t1, t3)
  28.     assertEquals(t1, t4, "this fails :(")
  29. }
Advertisement
Add Comment
Please, Sign In to add comment