Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
eternaldensity opened this issue Sep 4, 2019 · 4 comments
Labels
bug Something isn't working

Comments

@eternaldensity
Copy link

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
Copy link
Author

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 Tarmil transferred this issue from fsbolero/Bolero Sep 4, 2019
@Tarmil
Copy link
Member

Tarmil commented Sep 4, 2019

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
Copy link
Member

Tarmil commented Sep 4, 2019

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

@Tarmil Tarmil closed this as completed Sep 4, 2019
@Tarmil Tarmil added the bug Something isn't working label Sep 4, 2019
@eternaldensity
Copy link
Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants