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

前端 未结 3 1295
无人及你
无人及你 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条回答
  •  -上瘾入骨i
    2021-02-01 12:20

    Youn can use a package object and put the type declaration in there. When you import that package the type definition will be available.

    An example :

    //package myType
    package object myType {type mine = List[String]}
    

    Notice that the package object has the same name as the package in order to be usable without a object prefix i.e myType.mine If you name it with a different name for example :

    //package myType
    package object mainType {type mine = List[String]}
    

    it can be accessed with mainType.mine when you import "package"

    for more info : http://www.scala-lang.org/docu/files/packageobjects/packageobjects.html

提交回复
热议问题