FS0030, Value Restriction, is a topic that is often asked about. Recently Dmitry wrote a decent discussion of it here:

[link:blogs.msdn.com]

I would say the short summary is that

- if it looks like a 'value', it is not allowed to be 'generic' (no 'a in the type)

- if it looks like a 'function', then it is allowed to be generic

and 'cons1020' looks like a value, whereas 'newCons1020' looks like a function since it takes an argument 'f'.

By on 7/1/2010 10:55 PM ()

thank you~ ,i try understand it.

By on 7/1/2010 11:14 PM ()

Hello

As mentioned in <a class="internal-link

view-user-profile" href="[link:blogs.msdn.com] blog</a> when you write this code let v<'T> : 'T list ref = ref [] the compiler helps you out by creating a <i>type function</i>. That surprised me a bit too, particularly because I was expecting something a little more static.By using a class with a generic type parameter we can indeed get something a little more static (see below). I don't know if there are attributes or similar available to get the same result without the extra definition (StaticStorage<'T> in the code below).Compare the definitions of v<'T> and s<'T> in the following:/// Storage for a 'T list ref type StaticStorage<'T> () = static let a = printfn "Reserve ref cell for type %s list" typeof<'T>.Name; ref [] : 'T list ref static member refcell = a let v<'T> : 'T list ref = ref [] let s<'T> = StaticStorage<'T>.refcell let addToV x = v := x::!v let addToS x = s := x::!s To see the differences try to run these commandsaddToV 1 addToV "One" addToV 2 addToV 3 addToV "Two" addToV 4 addToS 1 addToS "One" addToS 2 addToS 3 addToS "Two" addToS 4 printfn "nonstatic<int> = %A" v<int> printfn "nonstatic<string> = %A" v<string> printfn "static<int> = %A" s<int> printfn "static<string> = %A" s<string> The result is shown hereReserve ref cell for type Int32 list Reserve ref cell for type String list nonstatic<int> = {contents = [];} nonstatic<string> = {contents = [];} static<int> = {contents = [4; 3; 2; 1];} static<string> = {contents = ["Two"; "One"];} I guess my point is to agree that some vigilance is due when writing <i>type function</i>s.I have a feeling that there must be a more elegant way to define a static value (of generic type) that I'm just not seeing. Please tell, if a more elegant solution is obvious to you all. [:)] Best regardsRobert Nielsen

By on 7/6/2010 11:38 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