BiasedScala: extension makes a right way

王福强

2021-10-15


I get to know extension is via Kotlin, After Scala3, extension comes to scala also. Cheerful, isn’t it?

extension is an elegant way to extend types and classes.

I use Commons-Lang3 library a lot to process string things, Let’s say, StringUtils.isEmpty(StringUtils.trimToEmpty(..)) or StringUtils.substringBetween(StrinUtils.trimToEmpty(..), .., ..), it’s necessary for precaution, but tedious, isn’t it? With Scala 3’s extension ability, we can make this more natural and elegant:

extension (s:String) {
    def isEmpty():Boolean = StringUtils.isEmpty(s)
    def trimToEmpty():String = StringUtils.trimToEmpty(s)
    def isEmptyAfterTrim():Boolean = StringUtils.isEmpty(StringUtils.trimToEmpty(s))
    def substringBetween(from:String, to:String) :String = StringUtils.substringBetween(s, from, to)
    ...
}

after that, we just use String as the way it is:

val str = "...."
if(str.isEmpty()) ...
if(str.isEmptyAfterTrim())...
val partWeWant = str.substringBetween(.., ..)

This’s so cool, and I like it this way.

We can also make extension generic, for example, we want our Money type has a display symbol when convert to string, then we can add an extension to it:

extension [M <: Money] (money: M) {
    def toStringWithSymbol():String = {
        money match {
            case USD => "$"
            case CNY => "¥"
            case GBP => "£"
            case _ => "#"
        } + money.amount
    }
}

With this extension, we can display meaningful information to our customers on GUIs or Pages of our applications:

val m:Money = ...
println(m.toStringWithSymbol) // usually pass with DTO

Formerly, if we want to extend some 3rd party libraries classes, we have to wrap them in some utility classes, especially these 3rd classes don’t expose enough priviledge to extend them.

Now, with extension of Scala 3, we just provide an extension to them, and everything goes like a charm.


>>>>>> 更多阅读 <<<<<<


欢迎加入「福强私学」

跨越2190个日夜,始终坚持“实践 + 原创”打造的715125字专属知识库,囊括了(但不限于)从职场、技术、管理与商业等多个板块的内容。

  • 一个ChatGPT触达不到的地方
  • 一个带你超越AI/人工智能的地方
  • 一个与你一起成长的地方

https://afoo.me/kb.html


开天窗,拉认知,订阅「福报」,即刻拥有自己的全模态人工智能。

订阅「福报」