Hi,
unless there is some issue with the optional arguments (I havn't tried it, but I don't think there can be ny), the following should work:

1
2
3
4
5
6
 
type Test(?parent: Test) as t = 
    let children = new ResizeArray<Test>()
    do match parent with
       | Some p -> children.Add(t)
       | None -> ()
By on 3/30/2008 3:42 PM ()

Hi Tomas,

My class instance was adding itself as a child to its parent's children collection, not to its own children.

Stephan

By on 3/30/2008 11:10 PM ()

Ah sorry, I overlooked that :-). This makes things a bit difficult, because you can't access local "let" bindings from other instances (which I think makes a good sense), so you'll probably have to expose "children" as a member (but thanks to the visibility annotations, it can be just a private member).

1
2
3
4
5
6
type Test(?parent: Test) as t = 
    let children = new ResizeArray<Test>()
    do match parent with
       | Some p -> (p.Children : ResizeArray<Test>).Add(t)
       | None -> ()
    member private t.Children = children

Unfortunatey it requires type annotation for the "p.Children" member (probably, because it needs to know the type in advance and it doesn't look at the members first... but I'm not exactly sure... Intuitively, I'd say it could work). Anyway, with the type annotation, it compiles fine.

By on 3/31/2008 6:23 AM ()

Thanks a lot. The required type annotation was the problem I stumbled over. I assumed one couldn't access members during initialization, but it seems like this is just a problem with type inference. It's still a bit odd, though.

Stephan

By on 3/31/2008 7:09 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