Ellen Spertus
11/20/2022, 12:59 AMJoffrey
11/20/2022, 1:01 AMval options = listOf(
Option("Option1", action = { foo(3) }),
Option("Option2", action = { bar(27) }),
)
Also, took the liberty to rename function
to action
, and add a trailing comma to make git diffs clearer when people add new optionsEllen Spertus
11/20/2022, 1:03 AMJoffrey
11/20/2022, 1:03 AMval options = listOf(
Option("Option1", ::doOption1Stuff),
Option("Option2", ::doOption2Stuff),
)
fun doOption1Stuff() = foo(3)
fun doOption2Stuff() = bar(27)
Ellen Spertus
11/20/2022, 1:18 AMJoffrey
11/20/2022, 1:19 AMEllen Spertus
11/20/2022, 1:19 AMJoffrey
11/20/2022, 1:19 AMEllen Spertus
11/20/2022, 1:20 AMJoffrey
11/20/2022, 1:21 AMOption
type, though, where you could create subclasses of options that implement the single action method, and those subtypes can take parameters via their constructorsEllen Spertus
11/20/2022, 1:21 AMprivate fun showPost(posts: List<RedditPost>, postNumber: Int) {
showPost(posts[postNumber])
val options = listOf(
Option("Show post author", { showAuthor(posts, postNumber) }),
Option("Check for comments", { checkForComments(posts, postNumber) }),
Option("Show next post", { showPost(posts, postNumber + 1) })
)
offerOptions(options)
}
Ellen Spertus
11/20/2022, 1:22 AMJoffrey
11/20/2022, 1:23 AMJoffrey
11/20/2022, 1:24 AMofferOptions {
option("Show post author") {
showAuthor(posts, postNumber)
}
option("Check for comments") {
checkForComments(posts, postNumber)
}
option("Show next post") {
showPost(posts, postNumber + 1)
}
}
But I guess that's far out of scope of such lessonColton Idle
11/21/2022, 2:13 PMColton Idle
11/21/2022, 2:13 PMColton Idle
11/21/2022, 2:14 PMEllen Spertus
11/21/2022, 4:16 PM