开发者

Is it possible in Scala to have two methods with identical signatures in the one class?

I would like to write a library such that clients could write code such as the following. It's in pseudo Scala since I don't know if it is syntactically possible

class ClientCode {

    historical {
        override def onTrade() { // historcal processing here}  
    }

    override def onTrade() { // real time processing here }
}

Basically what I would like to do is send trade data to the client. There is both historical and real time data which have different business rules. Syntactically I would like everything to be handled by onTrade(). I could do something like the following

override def onTrade() {

    if (historical) { } ...
    else {} 

}

However the historical rules are short and optional compared to the long and mandatory live rules so I find the above syntax a bit cumbe开发者_如何学运维rsome.


You can have a trait

trait Historical extends ClientCode {
  override def onTrade() { ... }
}

and then

val c = new ClientCode with Historical

to use the overridden onTrade(). Is that enough?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜