Question

I'm trying to match the Empty Guid using a Literal, and I can't figure out what's going on here:

let [<Literal>] EmptyGuid = System.Guid ()

let someFunction () = System.Guid.NewGuid () |> Some

match someFunction () with
| None           -> printfn "None"
| Some EmptyGuid -> printfn "Some EmptyGuid" 
  // ^ Comment this line out and it works! 
| Some guid      -> guid.ToString "D" |> printfn "Some Guid: %s"

When I try to run the program above, I get two different Exceptions somewhat randomly:

AccessViolationException was unhandled: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.

Or:

InvalidProgramException was unhandled: Common Language Runtime detected an invalid program.

This can't be my fault, can it? Either I'm incredibly stupid or there is something really weird going on...

EDIT:

I just noticed when which exception appears:

  • AccessViolationException when using .NET 4.5
  • InvalidProgramException when using .NET 4
Était-ce utile?

La solution

From the spec

· The right-hand side expression must be a literal constant expression that is made up of either:

· A simple constant expression, with the exception of (), native integer literals, unsigned native integer literals, byte array literals, BigInteger literals, and user-defined numeric literals.

—OR—

· A reference to another literal.

So I would think that your RHS does not follow the spec.

Neverthelss, I think you should be getting a more helpful error message. This should be reported as a bug to fsbugs@microsoft.com

Autres conseils

I'm not sure why that it lets you compile that at all. System.Guid is not an F# literal (see here). As John noted, you will need to do this with something other than a literal. Also, you might better off using System.Guid.Empty rather than System.Guid() if you want the default Guid.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top