Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does C# treat types which don't allow null from F# still nullable?

Tags:

f#

It is interop between C# and F#

In F#,

type test =
{
    value: int
}

type Wrapper (value: test) =
    member val Value = value with get, set

let trythis = new Wrapper(null)  // error as expected

However, in C#

 var trythis = new Wrapper(null);  //this runs fine
like image 215
colinfang Avatar asked Nov 01 '12 12:11

colinfang


People also ask

What is the main cause of hep C?

Hepatitis C is a liver infection caused by the hepatitis C virus (HCV). Hepatitis C is spread through contact with blood from an infected person. Today, most people become infected with the hepatitis C virus by sharing needles or other equipment used to prepare and inject drugs.

Does hep C go away?

Overview. Hepatitis C virus (HCV) causes both acute and chronic infection. Acute HCV infections are usually asymptomatic and most do not lead to a life-threatening disease. Around 30% (15–45%) of infected persons spontaneously clear the virus within 6 months of infection without any treatment.

What does hep C pain feel like?

Many people with chronic HCV suffer from aches and pains in their joints. A variety of different joints can be involved but the most common are in the hands and wrists. These pains are often minor but occasionally the pain can be quite severe. In such cases painkillers can be used to relieve the symptoms.

Does hep C treatment make you sick?

Treatment for hepatitis C keeps changing quickly. The standard treatment was typically interferon along with other drugs -- usually ribavirin and either boceprevir (Victrelis) or telaprevir (Incivek). But many people have a hard time with interferon's side effects, which include fatigue, fever, chills, and depression.


1 Answers

The non-nullable constraint on types is an F# specific feature and so it does not have any representation in .NET (and therefore C# does not respect it).

In fact, you can workaround this even in F# using an unsafe Unchecked.defaultof<_> value:

let trythis = new Wrapper(Unchecked.defaultof<_>) 

This is very useful if you want to check for null in an object that is exposed to C#:

type Wrapper (value: test) =
    if value = Unchecked.defaultof<_> then
      invalidArg "value" "Value should not be null."
    member val Value = value with get, set
like image 72
Tomas Petricek Avatar answered Sep 18 '22 23:09

Tomas Petricek