Hi Sergey,

It's not possible because when you update the field it needs to be exactly the same type as the field and in this case it is not. I believe the rule is the same of C# except you notice it less as you can explicitly choose the type of the list. Your solution is correct, I some times create a small auxiliary function to do the upcasting this generally save a bit of typing i.e.:

1
2
let uc x = x :> A
c.aList <- [uc (new B()); uc  (new B())]

Cheers,
Rob

By on 8/22/2008 8:01 AM ()

I was hoping that there maybe some kind of "untyped" list or "power" upcast command I've overlooked.

Oh well...

Thank you all for clarifications!

- Sergey

By on 8/23/2008 1:09 PM ()

What you are experiencing is not a problem with F# but a problem with static typing for which also other programming languages like Java or C# don't have a solution.

<b>Assuming that B inherits from A: list<B> can not generally be thought of as list<A></b> At least not in a traditional language and with mutable (and reference) operations. The problem is that your aList will be the same list as your bList (not a copy of it, aList and bList reference the same object) which would then suddenly allow you to add other A elements in your aList which also is a bList and you could no longer safely assume that your List<B> type list only contains B's. In Java one can use variables of type list<?> which may store any list and you can always be sure that what you take out of the list is an Object. (There are also more advanced constructs like List<? extends Shape>) Daniel

By on 8/22/2008 8:00 AM ()

This is known as co and contra varience and is not supported by F# (nor the CLR).

The details are a bit technical, so rathern than detailing them here let me point you towards a great blog series explaining this.

By on 8/22/2008 8:00 AM ()
IntelliFactory Offices Copyright (c) 2011-2012 IntelliFactory. All rights reserved.
Home | Products | Consulting | Trainings | Blogs | Jobs | Contact Us | Terms of Use | Privacy Policy | Cookie Policy
Built with WebSharper