fsbolero / TryFSharpOnWasm

F# Compiler running in WebAssembly with Bolero
Apache License 2.0
38 stars 3 forks source link

Compile errors on the F# tutorial from Visual Studio (System.Random.NextDouble missing?) #7

Closed eternaldensity closed 4 years ago

eternaldensity commented 4 years ago

I figured it would be fun and interesting to try running the F# tutorial code that comes as a new project type with Visual Studio, in the 'try F# in your browser' example at https://tryfsharp.fsbolero.io/

There's a few compile errors. I chopped out just the failing bits for brevity.

Code:


module TutorialFails = 
    let rnd = System.Random()
    let rec randomWalk x =
        seq { yield x
              yield! randomWalk (x + rnd.NextDouble() - 0.5) }

    let private parseHelper f = f >> function
        | (true, item) -> Some item
        | (false, _) -> None

    let parseDateTimeOffset = parseHelper DateTimeOffset.TryParse

    let result = parseDateTimeOffset "1970-01-01"

    let parseInt = parseHelper Int32.TryParse
    let parseDouble = parseHelper Double.TryParse
    let parseTimeSpan = parseHelper TimeSpan.TryParse

    let (|Int|_|) = parseInt
    let (|Double|_|) = parseDouble
    let (|Date|_|) = parseDateTimeOffset
    let (|TimeSpan|_|) = parseTimeSpan

    /// Pattern Matching via 'function' keyword and Active Patterns often looks like this.
    let printParseResult = function
        | Int x -> printfn "%d" x
        | Double x -> printfn "%f" x
        | Date d -> printfn "%s" (d.ToString())
        | TimeSpan t -> printfn "%s" (t.ToString())
        | _ -> printfn "Nothing was parse-able!"

Compile Errors from https://tryfsharp.fsbolero.io/ :

(7,41)-(7,51) The field, constructor or member 'NextDouble' is not defined. Maybe you want one of the following:
   Next
 (15,37)-(15,49) This expression was expected to have type
    'string * 'a * Globalization.DateTimeStyles'    
but here has type
    'string'    
 (30,10)-(30,16) Type mismatch. Expecting a
    'string -> 'a option'    
but given a
    'string * 'b * Globalization.DateTimeStyles -> DateTimeOffset option'    
The type 'string' does not match the type 'string * 'a * Globalization.DateTimeStyles'
 (31,10)-(31,20) Type mismatch. Expecting a
    'string -> 'a option'    
but given a
    'string * 'b -> TimeSpan option'    
The type 'string' does not match the type 'string * 'a'

The latter three errors look like a difference in how globalization is handled for datetimes between this version of .NET and what VS is using. (VS Community 2019 16.25.3 if that helps.) Probably not a big deal.

But .NextDouble() being missing from System.Random is rather odd. .Next() is there but of course it produces an int not a float.

I'm pretty sure the presence of System.Random.NextDouble() is something that should be available consistently.

eternaldensity commented 4 years ago

Now that I think about it... I should check Mono since that's what Blazor actually uses.

Okay, SystemRandom.NextDouble seems to exist in Mono too so I'm not sure why it won't work here.

Tarmil commented 4 years ago

This looks like a linkage issue: assemblies are stripped of unused code during compilation, unless they're listed here. We should keep the whole mscorlib, not just some specific type in it.

Tarmil commented 4 years ago

Yep, after applying this fix, NextDouble is now working. Thanks for the report!

eternaldensity commented 4 years ago

Ah, that makes sense. Cool, thanks, and glad to help.