Scala type keyword: how best to use it across multiple classes

前端 未结 3 1294
无人及你
无人及你 2021-02-01 11:36

Coming back to Scala after a spell writing Haskell, I\'ve started using the type keyword to make my class definitions a bit more readable, eg:

type RestfulParams         


        
3条回答
  •  醉话见心
    2021-02-01 12:34

    I'm not sure what constitutes niceness in this case, but here are two options:

    Using a trait: (Scala's Selfless Trait Pattern)

    trait RestfulTypes {
      type Params = Map[String,String]
      //...
    }
    
    object MyRestService extends RestService with RestfulTypes { /* ... */ }
    

    Or just import the object's members, effectively using it like a package:

    import util.RestfulTypes._
    

提交回复
热议问题