Created
May 3, 2010 16:14
-
-
Save mneedham/388258 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| let rec valueFor (property:AType) : obj = | |
| match property.Type with | |
| | AString -> box property.Name | |
| | AnInt -> box 0 | |
| | ADouble -> box 0.0 | |
| | ABoolean -> box false | |
| | ADateTime -> box DateTime.MinValue | |
| | AClass -> box <| build property.Type | |
| | _ -> box <| new obj() | |
| and build = | |
| log >> | |
| getShortestConstructor >> | |
| invokeWith (getParameters >> Array.map (fun p -> valueFor { Type = p.ParameterType; Name = p.Name })) >> | |
| andApply (fun t -> t |> getWriteableProperties |> Array.iter (fun p -> p.SetValue(t, valueFor {Type = p.PropertyType; Name = p.Name}, null))) | |
| This is the error: | |
| Warning 4 This and other recursive references to the object(s) being defined will be checked for initialization-soundness at runtime through the use of a delayed reference. This is because you are defining one or more recursive objects, rather than recursive functions. This warning may be suppressed by using #nowarn "40" or --nowarn 40. C:\mark\Builder\Builder.fs 42 62 Builder | |
| the 'valueFor' function call on the 'invokeWith' line gets underlined as being the problematic bit. | |
| If I define it like this there's no such error: | |
| let rec valueFor (property:AType) : obj = | |
| match property.Type with | |
| | AString -> box property.Name | |
| | AnInt -> box 0 | |
| | ADouble -> box 0.0 | |
| | ABoolean -> box false | |
| | ADateTime -> box DateTime.MinValue | |
| | AClass -> box <| build property.Type | |
| | _ -> box <| new obj() | |
| and build (t: Type) = | |
| getShortestConstructor t |> | |
| invokeWith (getParameters >> Array.map (fun p -> valueFor { Type = p.ParameterType; Name = p.Name })) |> | |
| andApply (fun t -> t |> getWriteableProperties |> Array.iter (fun p -> p.SetValue(t, valueFor {Type = p.PropertyType; Name = p.Name}, null))) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment