1

I wonder what's going on here...

I just created a new, empty F# Console application in Visual Studio 2013 (using F# 3.1 and .NET 4, FSharp.Core Version 4.3.1.0) and added the Reactive Extensions Main Library using Nuget: Install-Package Rx-Main

Now check this out:

Definition without new keyword

This works and the hovering over test shows val test: unit -> System.Reactive.Subjects.Subject<'a>. As expected. Then I added the new keyword.

Definition with new keyword

Interesting. Does anybody know why adding the new keyword breaks the code? For reference, if you additionally specify the type parameter, it works:

Definition with type parameters

Nikon the Third
  • 2,771
  • 24
  • 35
  • 1
    This can be demonstrated more easily by just defining a generic class `type MyClass<'a> () = do ()` and trying to instantiate it `let test = MyClass()` vs `let test = new MyClass()`. No need for Rx or anything disposable. Seems like a compiler idiosyncrasy. – Dax Fohl Jul 09 '14 at 15:50

2 Answers2

5

I can't find a spec reference off-hand, but when using new explicit type args are required. You need to do:

let test() = new System.Reactive.Subjects.Subject<_>()
Daniel
  • 47,404
  • 11
  • 101
  • 179
  • I tried finding the part of the specification that states this, but all I could come up with is [Section 6.4.2 - Object Construction Expressions](http://fsharp.org/specs/language-spec/3.0/FSharpSpec-3.0-final.pdf), which does not say that the arguments have to be explicit. – Nikon the Third Jul 10 '14 at 06:23
1

It appears to be a static class, and static classes cannot be newed up.

http://msdn.microsoft.com/en-us/library/system.reactive.subjects.subject%28v=vs.103%29.aspx

And to elaborate on your specific error message, it means there is public no constructor available that accepts 0 parameters. As far as I know, static classes only have private, parameterless constructors.

Kritner
  • 13,557
  • 10
  • 46
  • 72
  • 2
    Wrong `Subject`. He's using this one: http://msdn.microsoft.com/en-us/library/hh229173(v=vs.103).aspx – Daniel Jul 09 '14 at 15:49
  • In the OPs examples, wouldn't he be using *both* classes based on the code snippets provided? If not providing the type parameter, then the static class would have been used when attempting to new it up yes? – Kritner Jul 09 '14 at 15:59
  • No. The compiler recognizes that this is an instantiation expression and infers the omitted type args (when `new` is not present). – Daniel Jul 09 '14 at 16:02
  • Actually, @Kritner is right. :) The error message in the second picture refers to the static class. When I define a new generic class and try to instantiate it with `new`, the error message says: The type 'Test<_>' expects 1 type argument(s) but is given 0. – Nikon the Third Jul 10 '14 at 06:30