私のプロジェクトでは、複数のタイプを運ぶことができるリストが必要であり、キャストはしたくないので、obj list
sを使用してみました。動作するはずのコードの例を次に示しますが、何らかの理由で動作しません。
type Fruit() =
member this.Kind = "I'm a tasty fruit!"
type Apple() =
inherit Fruit()
member this.Kind = "I'm a crispy apple!"
type Cherry() =
inherit Fruit()
member this.Kind = "I'm a juicy cherry!"
> (new Fruit()).Kind
val it : string = "I'm a tasty fruit!"
... // And so on; it works as expected for all three fruits
> let aFruit = [new Fruit()]
val aFruit : Fruit list = [FSI_0002+Fruit]
> aFruit.Head.Kind // Works just fine
val it : string = "I'm a tasty fruit!"
> let fruits : obj list = [new Fruit(); new Apple(); new Cherry]
val fruits : obj list = [FSI_0002+Fruit; FSI_0002+Apple; FSI_0002+Cherry]
> fruits.Head // Okay, so we can extract items just fine. It also kept the property!
val it : obj = FSI_0002+Fruit {Kind = "I'm a tasty fruit!";}
> it.Kind // This doesn't work. Why? What am I missing?
error FS0039: The field, constructor or member 'Kind' is not defined