new keyword

61 views
Skip to first unread message

Robert Kuzelj

unread,
Oct 25, 2015, 10:23:19 AM10/25/15
to F# Discussions
Hi,

is there any specific reason why 'ordinary' classes must be instantiated via the 'new' keyword and now by simply calling the constructor function like with DUs?

It would make operating with external classes much more pleasant (from a F# POV).

so instead of

let v = new SomeView(Color = green)
let v' = SomeView(Color = green)

Best regards

Robert

Isaac Abraham

unread,
Oct 25, 2015, 10:25:05 AM10/25/15
to fsharp-o...@googlegroups.com

You don’t need to use the new keyword at all. Indeed in F#4 constructors are treated as first-class functions so you can use them within e.g. List.map etc..

 

The only time you’ll get a warning for not using the new keyword is for classes that implement IDisposable.

--
--
To post, send email to fsharp-o...@googlegroups.com
To unsubscribe, send email to
fsharp-opensou...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/fsharp-opensource
---
You received this message because you are subscribed to the Google Groups "F# Discussions" group.
To unsubscribe from this group and stop receiving emails from it, send an email to fsharp-opensou...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Robert Kuzelj

unread,
Oct 26, 2015, 4:38:58 AM10/26/15
to F# Discussions
Hey Issac,

thanks for that - I must admit I read that but forgotten about meanwhile.
Reason for that is that I am using Xamarin and will be stuck with f# 3.1 for some while. :-(

best regards

Robert

Loïc Denuzière

unread,
Oct 26, 2015, 10:56:14 AM10/26/15
to F# Discussions
You can actually call this:

    let v = SomeView(Color = green)

in F# 3.x too, it will work just fine.

What you do need F# 4 for is if you wanted to pass `SomeView` as a function, for example if you wanted to do something like this:

    let v = List.map SomeView [green; red; blue]

Robert Kuzelj

unread,
Oct 26, 2015, 11:47:29 AM10/26/15
to F# Discussions
Hi Loic,

    let v = SomeView(Color = green)

in F# 3.x too, it will work just fine.

Doesnt work for me in Xamarin ...


Reply all
Reply to author
Forward
0 new messages