Reputation: 1
I am trying to use a scala match expression as a switch statement. I do not have my case values stored in an array and want to generate it during runtime.
while (completedTasks < todoTasks) {
// Submit if machine 0 is ready.
if (status(0) == READY)
submit(0, 1)
// Submit if machine 0 is failed.
if (status(0) == FAILED)
submit(0, 1)
}
How can I convert the above piece of code into a match expression. I am trying to replicate a cloud server and run multiple threads to avoid communication overhead. Assumen having 10 machine.
Upvotes: 0
Views: 82
Reputation: 121
You can use pattern matching on the ADT Structure.
Here is one of the ways how you can do your example:
sealed trait Status
case object Ready extends Status
case object Failed extends Status
//Some logic to output READY or FALSE here.
// For example purposes we always return READY.
def status(value: Int): Status = Ready
status(0) match {
case Ready => "I'm ready"
case Failed => "I'm failed"
}
You can try out this example in a playground here.
Upvotes: 1