Cool bug. The C# compiler also doesn't generate an error but crashes at runtime, on both CLR and Mono.

Structs cannot be mutually recursive in general (how would storage be allocated), but I am not sure why this particular case doesn't work. I glanced at Partition II of the CLI specs and they seem to indicate it's OK when the field is a reference type (and ResizeArray is).

So, I guess it's safe to say that this isn't an F# bug (although an error would be nice), but a limitation in the runtime.

By on 4/13/2009 8:33 PM ()

Oh I think I see what the problem is -- the generics cycle. Node creates a type Range, parameterized by Node, but Node depends on Range. In fact, you don't even need to have storage in Range to trigger this problem; simply having the type parameter is enough:

1
2
3
4
5
6
 struct Range<A> { }

struct Node {
    public Node(Range<Node> s) { subnodes = s; }
    public Range<Node> subnodes;
}

This is because Range is a field on Node. If you remove this as a struct field, it'll be fine:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
 #light

type Range<'a> =
    //class
    struct
        val all_values : 'a ResizeArray
    end


type Node =
    struct
        val subnodes : Node Range ref // Remove ref for a crash
    end

let node = new Node()
printfn "%A" node

Could you elaborate on exactly why you need this?

By on 4/13/2009 8:44 PM ()

I'm writing a game for the XBox360 using XNA. The garbage collector of the .NET runtime on the xbox does not use generations. In order to keep garbage collection latency under control, one needs to either avoid allocating data, which is not really easy in F#, or keep the total number of GC-handled instances to a minimum. That's why I'm using structs instead of classes here. Using ref isn't really an option here, as references are subject to garbage collection.

By on 4/14/2009 6:25 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