I've tried the following code in VS2010:
open System.Security.Cryptography
let rsaTest1 =
let ecKey = [|0uy..143uy|] // junk data for testing
let ecKeyMod = ecKey.[8..8+128-1]
let ecKeyExp = ecKey.[136..136+8-1]
let rsa = RSAParameters(Modulus = ecKeyMod, Exponent = ecKeyExp)
rsa
let rsaTest2 =
let ecKey = [|0uy..143uy|] // junk data for testing
let rsa = RSAParameters(Modulus = ecKey.[8..8+128-1], Exponent = ecKey.[136..136+8-1])
rsa
If I highlight all code and send it to F# Interactive (Alt+Enter), then rsaTest1 works, but rsaTest2 gives an error message,
System.NullReferenceException: Object reference not set to an instance of an object.
at <StartupCode$FSI_0004>.$FSI_0004.main#() in P:\proj\Tachograph\Project\CompuTachTest\CompuTachTest\rsaTest.fsx:line 16
However, if I change rsaTest2 from a value into a function and call it,
let rsaTest2 () =
let ecKey = [|0uy..143uy|] // junk data for testing
let rsa = RSAParameters(Modulus = ecKey.[8..8+128-1], Exponent = ecKey.[136..136+8-1])
rsa
let x = rsaTest2 ()
then there is no error. F# bug or my mistake?
This is most likely a bug - if you compile the posted snippet with fsc and run it, you get this for x64:
Unhandled Exception: System.InvalidProgramException: Common Language Runtime detected an invalid program.
at <StartupCode$test>.$Test.main#()
and this for x86:
Unhandled Exception: System.InvalidProgramException: JIT Compiler encountered an internal limitation.
at <StartupCode$test>.$Test.main#()
You should report it via Microsoft Connect.
Related
I'm having a problem getting my DU working as expected. I've defined a new DU which either has a result of type <'a> or any Exception derived from System.Exception
open System
// New exceptions.
type MyException(msg : string) = inherit Exception(msg)
type MyOtherException(msg : string) = inherit MyException(msg)
// DU to store result or an exception.
type TryResult<'a, 't> =
| Result of 'a
| Error of 't :> Exception
//This is fine.
let result = Result "Test"
// This works, doing it in 2 steps
let ex = new MyOtherException("Some Error")
let result2 = Error ex
// This doesn't work. Gives "Value Restriction" error.
let result3 = Error (new MyOtherException("Some Error"))
I can't understand why it is allowing me to create an "Error" if I do it in 2 steps, but when i'm doing the same thing on a single line, I get a Value Restriction error.
What am i missing?
Thanks
UPDATE
Looking at the post by #kvb, adding type information each time I need to create an Error seemed a bit verbose, so I wrapped it up into an additional method which creates an Error and is a bit more succinct.
// New function to return a Result
let asResult res : TryResult<_,Exception> = Result res
// New function to return an Error
let asError (err : Exception) : TryResult<unit,_> = Error(err)
// This works (as before)
let myResult = Result 100
// This also is fine..
let myResult2 = asResult 100
// Using 'asError' now works and doesn't require any explicit type information here.
let myError = asError (new MyException("Some Error"))
I'm not sure if specifying an Error with 'unit' will have any consequences I haven't foreseen yet.
TryResult<unit,_> = Error(err)
Consider this slight variation:
type MyOtherException(msg : string) =
inherit MyException(msg)
do printfn "%s" msg
let ex = new MyOtherException("Some Error") // clearly, side effect occurs here
let result2 = Error ex // no side effect here, but generalized value
let intResults = [Result 1; result2]
let stringResults = [Result "one"; result2] // can use result2 at either type, since it's a generalized value
let result3 = Error (MyOtherException("Some Error")) // result would be of type TryResult<'a, MyOtherException> for any 'a
// In some other module in a different compilation unit
let intResults2 = [Result 1; result3] // why would side effect happen here? just using a generic value...
let stringResults2 = [Result "one"; result3] // likewise here...
The issue is that it looks like result3 is a value, but the .NET type system doesn't support generic values, it only supports values of concrete types. Therefore, the MyOtherException constructor needs to be called each time result3 is used; however, this would result in any side effects occurring more than once, which would be surprising. As Ringil suggests, you can work around this by telling the compiler to treat the expression as a value anyway:
[<GeneralizableValue>]
let result3<'a> : TryResult<'a,_> = Error(new MyOtherException("Some Error"))
This is fine as long as the constructor doesn't have side effects.
You can do:
let result3<'a> = Error (new MyOtherException("Some Error"))
EDIT:
As for why you can't do it in one step, first note that this results in the same error:
let result4 = Result (new MyOtherException("Some Error"))
As does this:
let result4 = Result ([|1;|])
But that this works:
let result4 = Result ([1;])
What's similar about Exception and Arrays, but not Lists? It's their mutability. The value restriction will bother you when you try to do make a TryResult with a type that is mutable in a single step.
Now as for why the two step process solves this, it's because the constructor make the whole function not generalizable because you're applying a function to the constructor. But splitting it into two steps solves that. It is similar to Case 2 here on MSDN.
You can read more about it at the above MSDN article and the why this happens in this more indepth blog post.
Can't deserializes JSON retrieved from a file. Get an unhandled exception of type 'System.TypeInitializationException' occurred in
let deserializedFiles = JsonConvert.DeserializeObject<Files list>(json1)
The code follows:
let foo = new JsonTextReader(new StreamReader(jsonFile))
let json1 = JsonConvert.SerializeObject(foo.Read())
let deserializedFiles = JsonConvert.DeserializeObject<Files list>(json1)
Thanks,
I have the following:
let GetDateTime() = System.DateTime.UtcNow
let InternalHandle dependencies =
let getDateTime = dependencies
let future = getDateTime()
let future = getDateTime().AddDays(float 5)
printf "The time is %A" future
()
let Handle() =
let dependencies = (GetDateTime)
InternalHandle dependencies
Handle();;
but I am compiler error with "getDateTime().AddDays(float 5)" regarding type annotation.
What do I need to do to get this to work?
getDateTime is a function and I cant add a type annotation.
I am baffled why it does not pick up its a function that returns a DateTime and therefore all functions like AddDays would be available
When doing type inference on functions, the F# compiler does not look at how the function is called later on. This means that when inferring type for InternalHandle, it only sees this:
let InternalHandle dependencies =
let getDateTime = dependencies
let future = getDateTime() // (#1)
let future = getDateTime().AddDays(float 5) // (#2)
printf "The time is %A" future
From this, it can infer that dependencies is a function (because it is called in #1), but it does not know what the function returns. When it gets to (#2), it sees that you want to invoke AddDays method on the result - but this is not enough to decide what the result type is (it could be any .NET object that has this method...). For this reason, you need a type annotation to specify what object are you expecting. You can write:
let InternalHandle (dependencies : unit -> System.DateTime) =
let getDateTime = dependencies
let future = getDateTime()
let future = getDateTime().AddDays(float 5)
printf "The time is %A" future
I am porting a VB.NET application to F# as an experiment. The VB program uses SQLDriverConnect, so I need to call it from F#. I cannot get the pinvoke/extern declaration to work properly. The call to SQLDriver connect always returns -2, SQL_INVALID_HANDLE, instead of prompting for a connection as expected.
Anybody know how to get this to work?
open System
open System.Runtime.InteropServices
open System.Text
[<DllImport("odbc32.dll")>]
extern Int16 SQLAllocEnv(IntPtr& EnvironmentHandle);
[<DllImport("odbc32.dll")>]
extern Int16 SQLDriverConnect(IntPtr hdbc, IntPtr hwnd, string szConnStrIn,
Int16 cbConnStrIn, StringBuilder szConnStrOut,
Int16 cbConnStrOutMax, Int16& pcbConnStrOut,
UInt16 fDriverCompletion)
let getConnectionString () =
let SQL_DRIVER_PROMPT = 2us
let mutable henv = IntPtr(0)
let mutable csLen = 0s
let rc1 = SQLAllocEnv &henv
assert (rc1 = 0s)
let csOut = new StringBuilder(1024)
let rc2 = SQLDriverConnect(henv, IntPtr.Zero, "", 0s, csOut, 1024s, &csLen, SQL_DRIVER_PROMPT)
assert (rc2 = 0s)
csOut.ToString()
[<EntryPoint>]
let main argv =
printfn "Connection string: %s" (getConnectionString())
0 // return an integer exit code
I don't do F# but in C you call SQLAllocEnv (or SQLAllocHandle) to create an environment handle, then you call SQLSetEnvAttr to set the version of ODBC you want, then SQLAllocConnect (or SQLAllocHandle) to allocate a connection handle and lastly call SQLDriverConnect with the connection handle. Your code looks to be passing an environment handle to SQLDriverConnect but SQLDriverConnect needs a connection handle hence SQL_INVALID_HANDLE.
I'm running into a bug in my code that makes me think that I don't really understand some of the details about F# and lazy evaluation. I know that F# evaluates eagerly and therefore am somewhat perplexed by the following function:
// Open a file, then read from it. Close the file. return the data.
let getStringFromFile =
File.OpenRead("c:\\eo\\raw.txt")
|> fun s -> let r = new StreamReader(s)
let data = r.ReadToEnd
r.Close()
s.Close()
data
When I call this in FSI:
> let d = getStringFromFile();;
System.ObjectDisposedException: Cannot read from a closed TextReader.
at System.IO.__Error.ReaderClosed()
at System.IO.StreamReader.ReadToEnd()
at <StartupCode$FSI_0134>.$FSI_0134.main#()
Stopped due to error
This makes me think that getStringFromFile is being evaluated lazily--so I'm totally confused. I'm not getting something about how F# evaluates functions.
For a quick explanation of what's happening, lets start here:
let getStringFromFile =
File.OpenRead("c:\\eo\\raw.txt")
|> fun s -> let r = new StreamReader(s)
let data = r.ReadToEnd
r.Close()
s.Close()
data
You can re-write the first two lines of your function as:
let s = File.OpenRead(#"c:\eo\raw.txt")
Next, you've omitted the parentheses on this method:
let data = r.ReadToEnd
r.Close()
s.Close()
data
As a result, data has the type unit -> string. When you return this value from your function, the entire result is unit -> string. But look what happens in between assigning your variable and returning it: you closed you streams.
End result, when a user calls the function, the streams are already closed, resulting in the error you're seeing above.
And don't forget to dispose your objects by declaring use whatever = ... instead of let whatever = ....
With that in mind, here's a fix:
let getStringFromFile() =
use s = File.OpenRead(#"c:\eo\raw.txt")
use r = new StreamReader(s)
r.ReadToEnd()
You don't read from your file. You bind method ReadToEnd of your instance of StreamReader to the value data and then call it when you call getStringFromFile(). The problem is that the stream is closed at this moment.
I think you have missed the parentheses and here's the correct version:
// Open a file, then read from it. Close the file. return the data.
let getStringFromFile =
File.OpenRead("c:\\eo\\raw.txt")
|> fun s -> let r = new StreamReader(s)
let data = r.ReadToEnd()
r.Close()
s.Close()
data