Skip to content

Commit

Permalink
add suugested extended enum puzzler (which also applies to any class-…
Browse files Browse the repository at this point in the history
…level extensions) #23
  • Loading branch information
angryziber committed Aug 13, 2017
1 parent a6d5504 commit 1507cdf
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
14 changes: 14 additions & 0 deletions src/types/extendedEnum/Rationale.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
Correct answer: ***d) will not compile***

* Extension functions on Color apply to instances of Color, e.g. `Color.Blue.from()`
* Extension function on the enum itself can be made only if it has a Companion object
```
enum class Color {
Red, Green, Blue;
companion object
}
fun Color.Companion.from(...)
```

Puzzler by Dmitry Kandalov @dkandalov
20 changes: 20 additions & 0 deletions src/types/extendedEnum/extendedEnum.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package types.extendedEnum

enum class Color {
Red, Green, Blue
}

fun Color.from(s: String) = when (s) {
"#FF0000" -> Color.Red
"#00FF00" -> Color.Green
"#0000FF" -> Color.Blue
else -> null
}

println(Color.from("#00FF00"))

// What will it print?
// a) Green
// b) Color.Green
// c) null
// d) will not compile

0 comments on commit 1507cdf

Please sign in to comment.