r/dotnet • • Aug 14 '18

Announcing F# 4.5

https://blogs.msdn.microsoft.com/dotnet/2018/08/14/announcing-f-4-5/
29 Upvotes

5 comments sorted by

1

u/sander1095 Aug 15 '18

As a main Asp.net/xamarin developer, what could I do in F# that I couldn't do in the C#? Or with which things is f# better?

2

u/ours Aug 15 '18

F# is from a different family of languages.

In theory it allows to write safer (more reliable) code with stronger checks (i.e. less likely throw a null reference exception).

C# is slowly adopting some of F# features like pattern matching which is a sort of souped up switch statement with strong compiler support to warn you of cases you aren't matching.

1

u/[deleted] Aug 15 '18

You might as well stick with C# - F# is the guinea pig and if the concept is awesome C# yoinks it.

2

u/JoelFolksy Aug 15 '18

Languages are not just lists of features. What isn't there is just as important as what is. C# can't keep "yoinking" features without getting heavy and bloated.

1

u/jpfed Aug 15 '18 edited Aug 15 '18

F# is way more concise when you want to define simple types with sane equality and hashing.

One neat feature of F# that is technically possible (but would be a pain) in C# is sum types (which are kind of like souped-up enums). Here's a quick example.

type CardSuit =
| Hearts
| Diamonds
| Clubs
| Spades  // looks like an enum so far

type CardRank =
| Number of int // but each case can store data!
| Jack  
| Queen
| King  

And record types:

type Card = {Rank: CardRank; Suit: CardSuit}

I didn't have to define .Equals or operator= to do things like

let message = 
    if myCard = yourCard then
        "Our cards match!"
    else
        "Our cards are different."

F# already has pattern matching (not sure if C# has it yet). Also, in C#, you can't use "var" and let C# infer the type of a lambda, but F# will happily infer the type of this function:

let isFaceCard card =
    match card.Rank with
    | Jack
    | Queen
    | King -> true
    | Number n -> false

F# lets you use the existing .NET collections classes but also comes with its own in case you prefer them:

let cardMap = Map.empty // no type args?
let cardsWithWeirdRules = 
    cardMap.Add(
        {Rank = Queen; Suit = Spades},
        "Actually worth 13 points"
    )

Since F# automatically defines sensible hashing and equality on record types, it's fine for me to use Cards as keys.