functional programming - how to create receive {} block in scala with no arguments -
i want have nice code following in scala:
class myclass { receive { case dothis => println("dothis") case dothat => println("dothat") } }
now understand receive should partial function, no matter partial function created didn't work. partial function should creating receive
noarg
code working.
or in other words code block should adding myclass can call
new myclass().receive(dothis) // code add myclass can call receive
thanks
if want create partialfunction
have specify type this:
sealed trait doparent case object dothis extends doparent case object dothat extends doparent { case dothis => println("dothis") case dothat => println("dothat") }: partialfunction[doparent, unit]
now can assign partial function (instance of partialfunction[doparent, unit]
) variable or field this:
val receive: partialfunction[doparent, unit] = { case dothis => println("dothis") case dothat => println("dothat") } receive(dothis) // dothis
Comments
Post a Comment