現在、何回かパス依存型で解決できるはずの問題が発生しましたが、それで最も完全で正しい方法で。 Javaの世界では、ネイティブ・タイプにマップされるクローズド・セットを定義する列挙型(静的な値の形で擬似列挙さえもある)を持つことは珍しいことではありません。私は彼らのネイティブ型にこれらの列挙型から私を取るいくつかのマッピングを提供したいと思いスカラは、パスに依存する型を持つネイティブ型に列挙します
public class Types {
/**
* <P>The constant in the Java programming language, sometimes referred
* to as a type code, that identifies the generic SQL type
* <code>BIT</code>.
*/
public final static int BIT = -7;
/**
* <P>The constant in the Java programming language, sometimes referred
* to as a type code, that identifies the generic SQL type
* <code>TINYINT</code>.
*/
public final static int TINYINT = -6;
/**
* <P>The constant in the Java programming language, sometimes referred
* to as a type code, that identifies the generic SQL type
* <code>SMALLINT</code>.
*/
public final static int SMALLINT = 5;
// ...
}
:java.sql.Types
が良い例です。私は手を前に、具体的なSqlType
インスタンスを取得し、適切なパス依存を取得するには、関数の引数としてそれを渡す必要があり、これは、しかし、少しグラグラです
import java.sql.{Types, ResultSet}
trait SqlType {
def typeValue:Int
type Value
def getValue(rs:ResultSet, idx:Int):Value
}
object SqlType {
object SqlInt extends SqlType {
type Value = Int
def typeValue = Types.INTEGER
def getValue(rs:ResultSet, idx:Int) = rs.getInt(idx)
}
object SqlString extends SqlType {
type Value = String
def typeValue = Types.NVARCHAR
def getValue(rs:ResultSet, idx:Int) = rs.getString(idx)
}
def getSqlType(typeValue:Int):SqlType = typeValue match {
case Types.INTEGER => SqlInt
case Types.NVARCHAR => SqlString
}
implicit class ResultSetExtras(rs:ResultSet) {
def getCell(idx:Int, sqlType:SqlType):sqlType.Value = sqlType.getValue(rs, idx)
}
}
:これまでのところ私はこのようなものを持っていますタイプ。
implicit class ResultSetExtras2(rs:ResultSet) {
def getCell2(idx:Int):SqlType#Value = getSqlType(rs.getMetaData.getColumnType(idx)).getValue(rs, idx)
}
は(SqlType#Value
の戻り値の型ではなく、パス依存sqlType.Value
の点に注意してください。(良くあります:私はこのような何かを行うことができますようだから、私が本当に欲しいものをしている、いないようです私は、シェイプレスやマクロのようなものが助けになるかもしれないと思っていますが、可能であれば、パスに依存するタイプ(あるいはシェイプレスでないものよりも実際にはマクロ)が可能かどうかを知りたいです。