Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does F# inherit every type from Object?

The question is simple and. although it is obvious the answer, I had to face a strange situation where the fsharp told me something a bit strange. Here's the story:

The question is: Does F# automatically make every type inherit the Object class? I suppose YES and I am certain of this because there would be so many complications if that were not the case.

But here's a fact. I was writing this piece of code:

type MyType =
   val myval: int
   override self.ToString = "Hello MyType"

Well, fsharp compiler told me it is not correct using overrid because he does not find any method called ToString to override. I so compiled this code:

type MyType =
   val myval: int
   member self.ToString = "Hello MyType"

And everything worked fine. mmmmmm What's happening??? Isn't FSharp supposed to inherit every object from Object?

like image 683
Andry Avatar asked Feb 14 '11 14:02

Andry


People also ask

What does f '( 1 mean in calculus?

Notation. The inverse of the function f is denoted by f -1 (if your browser doesn't support superscripts, that is looks like f with an exponent of -1) and is pronounced "f inverse". Although the inverse of a function looks like you're raising the function to the -1 power, it isn't.

Does f x mean Y?

The “Y” stands for the outcome, the “f” embodies the function used in the calculation, and the “X” represents the input or inputs used for the formula.

What does it mean if f is negative?

We can say that f is increasing (or decreasing) at an increasing rate. If f″(x) is negative on an interval, the graph of y=f(x) is concave down on that interval. We can say that f is increasing (or decreasing) at a decreasing rate.

What does f say about f?

What Does f ' Say About f ? The first derivative of a function is an expression which tells us the slope of a tangent line to the curve at any instant. Because of this definition, the first derivative of a function tells us much about the function.


2 Answers

You forgot parens:

type MyType =
   val myval: int
   override self.ToString() = "Hello MyType"

In your original code with parens omitted you were trying to override property ToString.

like image 109
desco Avatar answered Sep 19 '22 19:09

desco


Desco already explained the probelm with your snippet.

F# behaves just like C#, which means that all types inherit from Object, although with a slightly subtle behavior of value types. When you have a variable of a value type, it cannot be directly treated as Object and need to be boxed (to a .NET reference type that inherits from Object).

This can cause some confusing errors with type inference:

let foo x = 
  match x with 
  | :? System.Random as rnd -> rnd.Next()
  | _ -> 0

When you write this, you get an error message:

error FS0008: This runtime coercion or type test from type 'a to Random involves an indeterminate type based on information prior to this program point. Runtime type tests are not allowed on some types. Further type annotations are needed.

If everything inherited from Object in the strict sense, then F# compiler should be able to convert any type parameter to Object, but that's not allowed. The problem is that value types need boxing, so you need to write match box x with ....

like image 33
Tomas Petricek Avatar answered Sep 23 '22 19:09

Tomas Petricek