Curl Global Community

Full Version: なぜ String から BigDecimal にキャスト可能なのか
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
クラスの型が異なる場合、アップキャストでない限り、
asa での明示的なキャストは無効、という認識です。

しかし、Stringから、BigDecimal のキャストは成功しました。
これはなぜですか?不思議です。

Code:
{def tf = {TextField value = "123"}}
{value tf}
{CommandButton
    label = "キャスト",
    {on Action do
        def b1:#BigDecimal = tf.value asa #BigDecimal
        {popup-message b1}
    }
}
引数がひとつだけのコンストラクタ、もしくはファクトリに、
implicit というキーワードをつけておくと、
asa でキャストされる際に、適切な型のコンストラクタ、もしくはファクトリを探しだして、
自動的に、インスタンス化してくれるようですね。

知りませんでした。

Code:
{def tf = {TextField value = "123"}}
{value tf}
{define-class public serializable Dto
  field data:String
  {constructor public {default data:String = "data"}
    set self.data = data
  }
  {factory public implicit {from-String str:String}:Dto
    def dto:Dto = {Dto}
    set dto.data = str
    {return dto}
  }
}
{CommandButton
    label = "キャスト",
    {on Action do
        def d1:#Dto = tf.value asa #Dto
        {popup-message d1}
    }
}

Such cleaver work and reporting. Keep up the great works guys i've added you guys to my blog roll. This is a great .