Live data from Hacker News

Get Started with F# as a C# developer

blogs.msdn.microsoft.com

11–20 of 60 posts

Re: Get Started with F# as a C# developer

#11
post #6

Newbie here. Somebody can point to useful other resources for F#?

Is there a really good reason to choose to use F# over C#? Or does it really just come down to preference?

The returns on choosing F# are very much reliant of level of proficiency attained.

Once you are proficient, you will write

- less code for similar functionality

- have lot less dependency cycles

- using Option types over null helps in the long run

- improvement in productivity

The only problem is that you have attain a level of proficiency and flip the "train" of thought from OOP to Functional, and that can take some good time.

Re: Get Started with F# as a C# developer

#12
Are there any simple projects like HTTP server in F# for CoreCLR somewhere? With a Dockerfile if possible. Last time I tried to build something like that for PoC the amount of struggles I had with various versions of CoreCLR was overwhelming.

Re: Get Started with F# as a C# developer

#13
post #7

Ask HN: I've toyed around with F# and I kinda like it but I've never dug deep enough into it to answer the question: can I use this for 'real life' code as part of an existing C# desktop application for instance, e.g. build some F# into an assembly which is callable from C#? Without tons of hassle? What are good samples of real life application code in F#?

yes you can. C# / F# interop is not zero hassle but it is low hassle. Most of the time it just works. There are a few features in each language that are not zero hassle, if a C# api uses a custom implicit conversion, calling it from F# requires you to explicitly call the implicit conversion (ha!) and if an F# api returns something F# specific like an Option type, using it in c# can be a pain.

At my company we have a C# codebase for a large web application, but we create a 2 stage caching feature for it in F#. This F# project uses a couple of C# oriented libraries (stackexchange.redis and protobuf-net). So lots of interop going both ways.

Some examples of people using it in production: http://fsharp.org/testimonials/

Re: Get Started with F# as a C# developer

#14
post #7

Ask HN: I've toyed around with F# and I kinda like it but I've never dug deep enough into it to answer the question: can I use this for 'real life' code as part of an existing C# desktop application for instance, e.g. build some F# into an assembly which is callable from C#? Without tons of hassle? What are good samples of real life application code in F#?

> Without tons of hassle?

Well, yes and no. You definitely can do this, and for some applications it's worth it, but often the overhead at the API level will make you cringe.

APIs designed for C# are very different than APIs designed for F#, to account for partial application of functions, sum-types, non-nullability, immutability and value-like behavior by default from F# types, and also to make good use of F#'s type inference. Also, given stuff like the |> operator, making "fluent" APIs looks very different.

Using APIs made explictly for F# from the side of C# is somewhere between enormously annoying and de-facto impossible. The other way 'round, it's just massively annoying, as you lose many advantages of F#, and the code will not be much more compact as if you would write it from C#.

Pain-points in practice are around F# functions vs. C# methods and delegates, async/await vs. async-expressions, code quotations vs. Linq expressions, etc.

To be productive in F#, you want your API small and primitive, and stay within the language for the rest. If this is possible, go for it; if not, evaluate.

A final tip: Porting a big block from C# to F# is much less work than it sounds, and opens the door to refactoring that part into something sensible.

Re: Get Started with F# as a C# developer

#15

Newbie here. Somebody can point to useful other resources for F#?

Seconding the f# for fun and profit suggestions, especially the railway oriented programming section, I've also heard the following is good, but I have yet to try them:

http://www.fsharpworkshop.com/ http://products.tamizhvendan.in/fsharp-applied/

Re: Get Started with F# as a C# developer

#16
post #11
post #6

Earlier quoted context omitted.

Is there a really good reason to choose to use F# over C#? Or does it really just come down to preference?

The returns on choosing F# are very much reliant of level of proficiency attained. Once you are proficient, you will write - less code for similar functionality - have lot less dependency cycles - using Option types over null helps in the long run - improvement in productivity The only problem is that you have attain a level of proficiency and flip the "train" of thought from OOP to Functional, and that can take some…

Gotcha - thanks! I haven't touched functional languages other than for fun lately, so I may take a look sooner or later. I've always been interested.

Re: Get Started with F# as a C# developer

#17
post #5

Asynchronous programming with F# is pretty cool. It has layered powerful syntax and language constructs on top of .NET's threads and tasks, AND it gives you convenient access to the same async API that you use in C#.

performance is not good though, if you are trying to use it to leverage multiple cores.

Re: Get Started with F# as a C# developer

#18
post #12

Are there any simple projects like HTTP server in F# for CoreCLR somewhere? With a Dockerfile if possible. Last time I tried to build something like that for PoC the amount of struggles I had with various versions of CoreCLR was overwhelming.

Freya ?

This link may help you,

https://www.infoq.com/news/2017/05/freya-kestrel

Re: Get Started with F# as a C# developer

#19
post #6

Newbie here. Somebody can point to useful other resources for F#?

Is there a really good reason to choose to use F# over C#? Or does it really just come down to preference?

pros: less typing less null pointer exceptions

cons: performance (doesn't have to be bad but if you do things idiomatically will tend to be) tooling and other externalities (better that most niche languages but not as good as c#)

Re: Get Started with F# as a C# developer

#20
post #7

Ask HN: I've toyed around with F# and I kinda like it but I've never dug deep enough into it to answer the question: can I use this for 'real life' code as part of an existing C# desktop application for instance, e.g. build some F# into an assembly which is callable from C#? Without tons of hassle? What are good samples of real life application code in F#?

I can't share the code with you, but I did just that on a proprietary app for my company. Only the GUI shell of the app is in C#, and that only because the GUI builder features of Visual Studio can only generate C# code.

All of the "real" code in that app is in an F# library, which you add to the Visual Studio solution just like you would any other C# helper library. When you add a source file to that library, it's an F# source file, and it's built with the F# compiler into the library, which is then linked with the C# "main" program.

There are a few tricky parts, owing to the substantial differences between C# and F#. They all come down to the fact that there are aspects of both C# and F# that don't map naturally to the other. While there may be low-level .NET or IL ways to force the call, it's better to restrict your use of each language at the boundary between the two parts of the program.

Some examples:

1. F# has two ways of passing parameters to a function called the tuple and curry forms. Curried functions are the full-power form, but C# can't call them directly, so any function or method you want to export to the C# side must use the tuple form. At a superficial level, the only difference is some parentheses and commas, but at a low level, you're purposely nerfing the F# function to allow C# to call it. This is not an area you can avoid knowing about, since all of .NET uses tupled calling form, being written for C# initially.

2. F# has several native data types that have no direct implementation in C#. Some newer versions of C# have .NET extensions to allow them to manipulate values of these types given to the C# side from the F# side, but given a choice, it's often simpler to restrict the set of data types you define the interface between the two in terms of. That is, you generally don't want to allow discriminated unions, records, tuples, or function values through the boundary, if you can help it. Use data types C# copes with better: classes, structures, lists, and arrays instead.

3. C# code often makes use of null values, which are...well, "sinful" in F#. It is possible to use null in F#, but strongly recommended against, since F# is all about definite value states, and null adds an element of indefinite-ness to the application. You can choose either to allow null values through the C# to F# boundary or enforce the F# side's wish not to accept them at the boundary. Your choice.

4. Once you get into the F# way of doing things, you start to become more choosy about the C# libraries you use from F#, since some are more acceptable from an F# standpoint than others. You start to avoid libraries that have mutable values flying around everywhere, methods with side effects, etc. Some C# libraries are outright annoying to use, because you have to continually cast away so much of the F# value in order to use them. Fortunately, .NET itself is generally pretty good in this regard. The NuGet world shows less restraint here.

Post reply on HN