type CountryCode = String @@ CountryCodeTag
+ String @@ CountryCodeTag
がString
から、すなわちすべてのメソッドを直接使用することができ、String
のサブタイプです。
− String @@ CountryCodeTag
は、String
が予期される場所で誤って使用される可能性があります。つまり、タイプセーフではありません。
−新しい値を作成するのは少し厄介です:"a".asInstanceOf[String @@ CountryCodeTag]
またはval tagger = new Tagger[CountryCodeTag]; tagger("a")
です。
−シェイプレス依存(手動でも可能ですが)。
class CountryCode(code: String) extends AnyVal
+これは、より多くのタイプセーフです。 String
から
−方法は、いくつかの余分な努力と用意されています
class CountryCode(val code: String) extends AnyVal
new CountryCode(countryCode.code.toUpperCase)
または
class CountryCode(val code: String) extends AnyVal
object CountryCode {
def unapply(...) = ...
}
countryCode match { case CountryCode(code) => new CountryCode(code.toUpperCase) }
または
case class CountryCode(code: String) extends AnyVal
countryCode.copy(code = countryCode.code.toUpperCase)
+は、新しい値を作成もう少し自然です:new CountryCode("a")
。
+特別な依存関係はありません(これはScalaです)。
短所の例を教えてください。 '' 'String @@ CountryCodeTagは、いくつかのStringが期待される場所で誤って使用される可能性がありますか? – gyoho
@gyoho 'val countryCode:String @@ CountryCodeTag = ???' 'case class Person(name:String)' 'Person(countryCode)' –
メモリフットプリントの観点から、どちらが良いと思いますか? – gyoho