Are float array, float [] and double [] different or the same?

I write my code as if it is all the same and without problems, but it starts to confuse me when I was imposing a function in Visual Studio and I see that type definitions contain 3 different types, which I thought, anyway . They are the same? Or are they different?

+7
f #
source share
2 answers

They are the same. See Type Abbreviations at the bottom of the FSharp.Core Documentation . float = double = System.Double and array<'T> = 'T[] . You can also define your own type abbreviations and use them the same way:

 type dbl = double let (d:dbl) = 1.0 

You did not ask about this, but note that one place where type abbreviations may not work as you expected is measure types; float<_> is defined independently of float , double and System.Double , and there is no such thing as the corresponding double<_> or System.Double<_> .

+11
source share

In addition to type abbreviations, there are two useful things you need to know about F # types. First of all, there are two ways to write generic type names. One way is to use the OCaml syntax, and a second way is to use the .NET syntax:

  • When using the .NET syntax, you write array<'T> or, for example, OtherType<'T1, 'T2> (for types with more than one type parameter).
  • In OCaml syntax, the same thing is written as 'T array or ('T1, 'T2) OtherType

The two entries are equivalent - when you declare a type value annotated using the .NET syntax, you can assign it a value annotated using the OCaml syntax. 'T[] are special notations for arrays, but this explains why array<'T> matches 'T array .

Secondly, F # uses the bad name bit for floating point types. This is probably due to compatibility with OCaml, but this can easily confuse .NET programmers:

  • The F # float type corresponds to System.Double in .NET (which is called double in C #)
  • F # float32 type corresponds to System.Single in .NET (which is called float in C #)

As @kvb points out, double is another type alias for the System.Double type.

+7
source share

All Articles