私の傾向は、パッケージ オブジェクトに暗黙を配置することです。
作業がパッケージ com.acme.mydsl で定義されるとします。ソース ファイルは、ディレクトリ階層 com > acme > mydsl に配置されます。ディレクトリ mydsl で、次のようにオブジェクトを定義します。
package com.acme; //we are in the mydsl dir, but note no mydsl in
//in the package declaration
package object mydsl {
implicit def idioticallyStringsAre5( s : String ) : Int = 5
//define other utilities that should be available within
//the package or importable
}
これで、次のことができます。
scala> import com.acme.mydsl._
import com.acme.mydsl._
scala> def sum( a : Int, b : Int ) = a + b
sum: (a: Int, b: Int)Int
scala> sum("hello", "there")
res0: Int = 10
import com.acme.mydsl._ をインポートすることにより、暗黙的な変換を含むすべてのパッケージ レベルの関数定義を取得しました。
パッケージオブジェクトがとても好きです。ユーティリティ関数のためだけにクラスを静的メンバーでいっぱいにしなければならないのは、Java では常にばかげているように思えました。パッケージ オブジェクトは、暗黙的な変換を含め、これらのユーティリティの非常に洗練された名前空間として機能します。