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:
This works and the hovering over test shows val test: unit -> System.Reactive.Subjects.Subject<'a>. As expected. Then I added the 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:
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<_>()
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.
Related
The following gives a runtime error on the last line but why do I not receive a compile time error?
Why is this? fnSub (last line) accepts a type of Sub but here I'm passing it a type of Parent and it compiles. Ok, I do get a runtime error but I'd have thought this should have given me a compile time error. Is this a bug in Dart or am I misunderstanding the limitations of the type system or have I just gone crazy?
class Parent {}
class Sub implements Parent {
String get blah => "blah";
}
String fnSub(Sub sub) => sub.blah;
String aProblem(Parent parent) => fnSub(parent);
https://dartpad.dev/acd2767cd42371deae0644fa66e8c602
The problem is that implicit-casts are enabled by default in Dart which is a feature trying to make it easier to work around types in Dart by automatically adding type casts in your code base.
This feature will no longer be available when NNBD (Non-nullable by default) are coming where implicit-dynamic also will be gone. Both features can already be disabled today by following this guide: https://dart.dev/guides/language/analysis-options#enabling-additional-type-checks
Personally, I think most projects should disable this two features already since I have seen a lot of people on Stackoverflow being confused about what Dart are doing with the types. So I cannot wait for NNBD so we can get are lot more clear type experience in Dart. And hopefully, the errors from the analyzer will be clear enough for most people so they don't need to get help.
If you disable implicit-casts you code will fail at the following line:
String aProblem(Parent parent) => fnSub(parent);
And with this error:
error - The argument type 'Parent' can't be assigned to the parameter type 'Sub'. - bin\stackoverflow.dart:9:41 - argument_type_not_assignable
If you want to test with Dartpad you can try with the following edition based on a beta version of the next Dart version which has enabled null-safety (and therefore have no implicit-casts): https://nullsafety.dartpad.dev/
I created an F# 'Nunit' project using a template in VS 2019 to do some testing, and the default fixture code it created was the following:
namespace Tests
open NUnit.Framework
[<TestClass>]
type TestClass () =
[<SetUp>]
member this.Setup () =
()
[<Test>]
member this.Test1 () =
Assert.Pass()
It's an otherwise ordinary looking class, except the curiosity on the class declaration. I expected to see [<TestFixture>], but instead I see [<TestClass>], the same name as the auto-generated type. I suspect that this is a bug in the project template, but strangely, F# will happily compile this, with a note that it probably won't be compatible with other .Net languages:
warning FS3242: This type does not inherit Attribute, it will not work correctly with other .NET languages.
crystal clear for me. My question is, why does F# even allow this? Do the reflection capabilities in .Net even work for such an attribute in F#? What would a potential use-case be?
Historically the check was forgotten to be added to the compiler. When it was discovered, I propose to make it compilation error, but it ended up as a warning for backward compatible reasons. See https://github.com/Microsoft/visualfsharp/pull/5192 and https://github.com/Microsoft/visualfsharp/pull/5610 for details.
Alas, I do not have the answer to the question why F# allows this. A quick experimentation shows that using ordinary classes as attributes works - at least in a very basic situation:
type A(n:int) =
member x.N = n
[<A(10)>]
type B() = class end
(typeof<B>.GetCustomAttributes(true).[1] :?> A).N
I have a union type that has a single, empty case.
type Default =
| Default
This type has a purpose, but it's not meant to be visible or usable.
Unfortunately, I have to use it in an inline function that does need to be visible. This prevents me from making the type or the case private.
The solution I came up with is using the CompilerMessageAttribute on it to signal an error whenever it's used. This would be fine, but now I can't compile my own assembly because IT uses it.
Is there a way to signal an error only when it's used by an assembly that references my assembly?
Let me reiterate the requirements to make sure I understand them:
The type needs to be public, so that that other assemblies can reference it implicitly via inline.
But if other assemblies reference it explicitly, then that is an error.
I don't know of any way of doing this using standard tooling.
I can see two possible solutions.
If only one calling assembly needs to use the inline function, what about making the type internal and then have the calling assembly be a friend assembly, using the InternalsVisibleToAttribute.
The only other alternative I can think of is security by obscurity. Hide the type in some awkwardly named module and require module qualification. This will stop accidental use of the type, if nothing else.
You could even add a build step to check that no source code references the module name.
[<RequireQualifiedAccessAttribute>]
module ``Dont Use This`` =
type Default =
| Default
let x = ``Dont Use This``.Default
And yes, it's very kludgy.
My question is really a repeat of an old question posted here:
Ninject 2.2 multiple bindings
It seems someone was going to deal with this back in 2011. Does anyone know if there is some way to turn off such warnings in Ninject? Or some other workaround?
EDIT
In response to #BatteryBackupUnit, here is my exact problem:
I have multiple libraries... and in my core library, I do something like this:
Find all assemblies referenced by the host application (including the host)
Find all types inheriting from IDependency from all those assemblies.
Automatically register all of those as transient
Then from another library (which may or may not be referenced by the host app), I have this:
Kernel.Bind<IDbContextFactory>().To<DbContextFactory>().InSingletonScope();
Here IDbContextFactory is also an IDependency, so it got loaded already by the core library and now I register it here but with a different scope (singleton).
From experience (and having tested it earlier) I know this is no problem in Autofac, but Ninject gives me that error message about having already registered it.
Ideally it would be better to just override any previous registrations... "cascade style" (for lack of a better phrase)..
Ninject does now support overriding open generic bindings with more specific ones.
For Example:
public interface IFoo<T> { }
public class Foo<T> : IFoo<T> { }
public class StringFoo : IFoo<string> {}
used like:
var kernel = new StandardKernel();
kernel.Bind(typeof(IFoo<>)).To(typeof(Foo<>));
kernel.Bind<IFoo<string>>().To<StringFoo>();
var intFooInstance = kernel.Get<IFoo<int>>();
var stringFooinstance = kernel.Get<IFoo<string>>();
Works.
However, if you're not talking about open generic bindings, ninject 3 still handles multi bindings the same as ninject 2.2.
In most scenarios you can work around this by using contextual bindings. Okay i would not exactly call it a workaround, i would call it good design.
In general this is described here: https://github.com/ninject/ninject/wiki/Contextual-Binding
A simple way would be to specify the binding using a name. This requires one binding for the specified one and allows only one, too.
See: https://github.com/ninject/ninject/wiki/Contextual-Binding#simple-constrained-resolution-named-bindings
It is also possible to define a "default" binding like .Bind<IFoo>().To<Foo>(); and special case bindings with the .When(...) syntax, like:
.Bind<IFoo>().To<SpecialFoo>().When(ctx => ...)
See https://github.com/ninject/ninject/wiki/Contextual-Binding#specifying-constraints-on-the-type-binding-using-arbitrary-elements-of-the-resolution-request-context
If you show us your concrete problem we might be able to provide a more concrete solution.
I tried following the example given on MSDN, but my code does not compile on the compact framework. It does compile on the normal framework, though.
type StorageComponent(game) =
inherit GameComponent(game)
let title_storage_acquired_event = new Control.DelegateEvent<StorageEventHandler>()
Error message:
The type 'DelegateEvent' is not defined
Based on the hint from Brian, it looks that the types DelegateEvent<'Delegate> and Event<'Delegate, 'Args> are not supported on .NET Compact Framework. This would mean that you cannot declare an event that uses an explicitly specified delegate type.
However, you can still use the Event<'T> type which creates an event of type Handler<'T> (which is a generic delegate type representing methods with two parameters of types obj and 'T):
type StorageComponent(game) =
inherit GameComponent(game)
let titleStorageAcquiredEvent =
new Event<StorageEventArgs>()
[<CLIEvent>] // If you want to create C# compatible event
member x.TitleStorageAcquired =
titleStorageAcquiredEvent.Publish()
Assuming that the declaration of StorageEventHandler looks like this:
delegate void StorageEventHandler(object sender, StorageEventArgs args);
The example above should create more or less an equivalent code (with the only difference that it uses generic Handler<_> delegate type instead of your own StorageEventHandler).
If you take a look at the code in the CTP
C:\Program Files (x86)\FSharp-2.0.0.0\source\fsharp\FSharp.Core\event.fs
it will offer some clues (I'm guessing NETCF does not have del.DynamicInvoke). It might also offer clues as to what to do instead; I'm not sure, but hopefully someone else will chime in with a full answer.