News for hackers. Stuff that matters.
Re:F# is gaining independence from .NET by hvidgaard on Monday February 01, 2021 @03:01AM (#25986563)
It's a real shame, but not unexpected. The vast majority of programmers I meet in the wild simply do not have training in FP and by the time they decide to learn it (if they do), it's hard to unlearn "traditional" thinking.

If we want to steer programming in that direction, we should teach functional programming before "traditional" programming. However, even if we manage to do that I'm not convinced that it will make a different. People think procedural and it seems that the abstraction of first order functions is hard.

Re:F# is gaining independence from .NET by dleslie on Monday February 01, 2021 @03:19AM (#25986662)
I'm a long-time Emacs user, Scheme fan, functional programming advocate ... Who uses C# in his day job.

For quite a long time F# simply wasn't ready, and by the time it was C# had already become an industry behemoth.

But more critically, it's not a language that's officially supported by Unity. That's enough for it not to be a wise choice to be betting a project on it where there are dozens of jobs on the line, and families that could be impacted.

I can't help but think that this concern isn't simply isolated to Unity software development; where else has the decision been made to not use F# on account of it not enjoying as much industry support as C#?

Re:F# is gaining independence from .NET by xupybd on Monday February 01, 2021 @03:55AM (#25986860)
I can't say that I've ever known anyone who uses Unity professionally but if that's your line of work it would be a big factor.
Re:F# is gaining independence from .NET by pjmlp on Monday February 01, 2021 @09:49AM (#25989118)
Sitecore, SharePoint, all the GUI designers for Forms, WPF, UWP, WinUI, EF (Core), WCF, gRPC, upcoming Code Generators.
Re:F# is gaining independence from .NET by dmitriid on Monday February 01, 2021 @04:16AM (#25986997)
I've done FP (several years of Erlang), and I use FP daily in my work (since most languages support FP to varying degrees).

I see no point in F# other than "well, it's a nice-ish ML for .NET", and the article does nothing to help with that (file order? really? that's what you're going for as the first point to make about a language?) .

Re:F# is gaining independence from .NET by KurtMueller on Monday February 01, 2021 @02:09PM (#25992269)
Well, now it's also a "nice-ish ML" for javascript, thanks to Fable.
Re:F# is gaining independence from .NET by catmanjan on Monday February 01, 2021 @03:06AM (#25986592)
As a .NET developer I don't see any reason to use F# professionally. C#/CPP are tools just like a hammer, F# is a different hammer, it doesn't seem to offer advantages in my field other than being different...
Re:F# is gaining independence from .NET by freeone3000 on Monday February 01, 2021 @03:14AM (#25986633)
It allows you to envision your application as a series of data transforms, instead of a list of steps. This allows much more proximal abstractions for tasks involving data manipulation.
Re:F# is gaining independence from .NET by dleslie on Monday February 01, 2021 @03:19AM (#25986669)
You can treat your application as a series of data transforms with C#, as well.
Re:F# is gaining independence from .NET by freeone3000 on Monday February 01, 2021 @03:31AM (#25986726)
It's.. not as nice. LINQ gets halfway, but, you end up getting back to procedural modeling soon. With sufficient discipline and structure, and liberal use of LINQ and lambdas, you can mostly get there, but it lacks the purity of pure functional.
Re:F# is gaining independence from .NET by Stolpe on Monday February 01, 2021 @03:52AM (#25986850)
But F# isn't a pure functional language, is it? https://www.google.com/search?q=is+f%23+a+pure+functional+la...
Re:F# is gaining independence from .NET by dleslie on Monday February 01, 2021 @03:44AM (#25986796)
And in the process you've incurred a devastating amount of micro-allocations. When using Unity with an older version that doesn't have incremental GC this is a serious problem.

Does F# avoid that?

Re:F# is gaining independence from .NET by freeone3000 on Monday February 01, 2021 @04:29AM (#25987077)
Oh, god no. Performance even on fairly trivial number crunching tasks is half of C#, nevermind anything where GC is important or you'd consider writing it native. It's not a performant language, that's not the point.
Re:F# is gaining independence from .NET by moonchild on Monday February 01, 2021 @04:36AM (#25987119)
That's not a fundamental limitation, though. Implementations of SML (f# is effectively an ML implementation), OCaml, and Haskell are all quite performant.
Re:F# is gaining independence from .NET by buybackoff on Monday February 01, 2021 @05:52AM (#25987538)
F# generates weird, verbose, opinionated IL code that is often JIT-unfriendly and rather focused on reference types. JIT is tailored to C# patterns and JIT is rather simple in .NET (e.g. vs JVM), so it's easy to break or miss some optimizations.

Yes, it's not a fundamental limitation of the language itself. But considering the amount of hypothetical efforts required to fix all that vs investments from MSFT in F#, it is fundamental. F# lags by several years behind JIT and TPL improvements, the issues are mostly known. But it feels like it's not that important for F#. It's is not for writing fast libraries, but for Python-like use cases, explorative programming, or writing code fast when performance is not important (no hot loops in F# code) but strict typing discipline makes code less buggy and more maintainable from the first attempt. They used to say that "if F# code compiles, it's probably correct" - that is close to the truth, but do not expect top performance without fighting with the F# compiler. Even one of the best known and oldest F# library FParsec has it's high-performance parts in C# - that's telling.

Re:F# is gaining independence from .NET by piaste on Monday February 01, 2021 @06:44AM (#25987808)
> Even one of the best known and oldest F# library FParsec has it's high-performance parts in C# - that's telling.

To be more specific, it has a C# library so it can use _unsafe {}_ blocks to perform manual memory management. This is one key performance-oriented feature that F# lacks.

Other than that, you _can_ write F# that's about as fast as non-unsafe C#, although it means giving up most of the nicer and safer features of F#. Use mutable structs and classes instead of immutable records, for/while loops instead of higher-order functions, magic values instead of option/result types, etc... I've done it in a couple of hot paths where it made sense, but quite reluctantly.

Inline functions are still fine though for writing zero-cost helpers, and I don't think C# has them (there's an <AggressiveInlining> attribute but IIRC it's just a hint, the compiler isn't required to actually obey).

Ironically, F# _could_ become the best language for high-performance .NET programming, because it supports directly inlining IL instructions intermixed with regular code (whereas C# has to go through the ILGenerator class). Unfortunately it's considered such a niche case that it's not planned to be expanded beyond the standard library, and to use it in your code you have to enable an undocumented / unsupported compiler flag.

Re:F# is gaining independence from .NET by buybackoff on Monday February 01, 2021 @07:47AM (#25988145)
> Inline functions are still fine though for writing zero-cost helpers, and I don't think C# has them (there's an <AggressiveInlining> attribute but IIRC it's just a hint, the compiler isn't required to actually obey).

In F#, you cannot disable inlining of small functions. It does IL source code inlining, like copy-paste, not machine code inlining. That increases generated dll/exe size a lot for generics. And sometimes you do not want to do that because it's worse for performance. There is an issue for that: https://github.com/fsharp/fslang-suggestions/issues/838

In C#, the AggressiveInlining attribute works well, in predictable manner. Non-inlineable cases are well known (`throw\switch\fixed\try..catch\calling delegates` and some more https://github.com/dotnet/runtime/blob/master/src/coreclr/ji...).

> because it supports directly inlining IL instructions intermixed with regular code

This works for very simple things only. And using InlineIL.Fody, Sigil, raw IL.Emit or just raw IL code as text is not that more difficult if you already know IL.

> you _can_ write F# that's about as fast as non-unsafe C#

I tried this several times. It always ends with fighting the compiler more than just rewriting in C#, even without the `unsafe` keyword. In most cases due to generated IL that I cannot control from F#.

Re:F# is gaining independence from .NET by Smaug123 on Monday February 01, 2021 @09:08AM (#25988795)
F# does a good job with tail-recursion, though - you can make the for/while loop safer this way. https://www.gresearch.co.uk/article/in-favour-of-recursive-f...
Re:F# is gaining independence from .NET by buybackoff on Monday February 01, 2021 @09:27AM (#25988940)
But it adds .tail prefix before every callvirt instruction and this breaks some JIT optimizations. E.g. generic type specialization 'if(typeof(T) == typeof(...))' breaks, I had to dig into compiler source code to find out that it doesn't add .tail before ctors, used a struct wrapper and called a method in the struct ctor to avoid .tail prefix, but it adds its own overhead.

Also, most simple tail calls are rewritten by a compiler to a loop, but if you look at the generated IL, there are multiple temp variables, locals, needless assignments. Maybe JIT is able to eliminate those, or maybe not... In C# I could get IL that reflects what I write, without surprises.

Re:F# is gaining independence from .NET by buybackoff on Monday February 01, 2021 @10:36AM (#25989546)
Slight correction: .tail prefix prevents inlining by JIT, it's mentioned in the second link in my sibling comment as "explicit tail prefix in callee". So even if that type specialization JIT optimization worked, the small performance-critical method containing it was not inlined. But the point of the specialization was to make the call for primitives faster than a method call, and fallback to a default implementation via callvirt. And there is no way to disable adding the .tail prefix to the calls that do not need it.

This also means that a lot of F# functions that are not inlined by F# source-code inlining itself effectively have NoInlining attribute. E.g. some helper method `if x then y.call() else z.call()`, if not inlined by F#, will never be inlined by JIT due to .tail prefixes before the methods calls.

Re:F# is gaining independence from .NET by 29athrowaway on Monday February 01, 2021 @05:48AM (#25987516)
You can use a teaspoon instead of a spoon to eat soup. That doesn't mean it's the best utensil for that specific task.
Re:F# is gaining independence from .NET by pjmlp on Monday February 01, 2021 @08:16AM (#25988349)
Without the Visual Studio tools for GUI and database modeling, so it ends up being just usable for writing general purpose libraries.

So, we just carry on using VB.NET, C# and C++ instead.

Re:F# is gaining independence from .NET by chrisseaton on Monday February 01, 2021 @08:40AM (#25988560)
Isn't 'writing general purpose libraries' most programming? I think GUIs and database modelling are pretty niche.
Re:F# is gaining independence from .NET by pjmlp on Monday February 01, 2021 @08:47AM (#25988610)
Code isn't written in isolation unless we are speaking about programming interviews.

Libraries spring to life as reusable parts from large scale projects.

So the ROI of context switching to F# in the middle of a project, isn't that much if the language is reduced to expressing algorithms.

Re:F# is gaining independence from .NET by oblio on Monday February 01, 2021 @03:26AM (#25986708)
> proximal abstractions

Proximal? Maybe you mean appropriate? Or incremental?

Re:F# is gaining independence from .NET by sfg on Monday February 01, 2021 @06:10AM (#25987631)
Re:F# is gaining independence from .NET by xupybd on Monday February 01, 2021 @03:10AM (#25986617)
The functional hammer can be a little easier to reason about. So you can understand your application a little more.
Re:F# is gaining independence from .NET by dleslie on Monday February 01, 2021 @03:20AM (#25986670)
It's possible to write functional C# code, as well.
Re:F# is gaining independence from .NET by paavohtl on Monday February 01, 2021 @04:42AM (#25987164)
Ehh, not really. It depends on how you define functional programming. C# supports first class functions, tuples, some form of pattern matching and LINQ extensions support basic FP collection operations like map (Select), filter (Where) and fold (Aggregate). But I think that's just scratching the surface compared to a "real" FP language:

- F# has function composition operators

- F# functions are curried and support automatic partial application

- F# variables and data structures are immutable by default

- F# has powerful global HM type inference

- F# uses option & result monads over exceptions and null

- F# has algebraic data types with exhaustiveness checking

These capabilities make writing code in F# a very different experience from C#. Some of these can be added to C# as features, but many things are fundamental parts of language design that are hard to change afterwards.

Re:F# is gaining independence from .NET by dleslie on Monday February 01, 2021 @10:36AM (#25989551)
That stinks of microallocations from enormous stacks of closures, boxing and similar.
Re:F# is gaining independence from .NET by xupybd on Monday February 01, 2021 @03:46AM (#25986814)
Yeah you can. I find it a little more clunky. Many of the things I prefer are just syntactic sugar. But if I'm honest I really like syntactic sugar.

On top of that I find functional first languages are great for forcing you to learn functional styles that you can later user in languages like C#.

C# is a brilliant language and I wouldn't knock it. I'd use that as a first option at work but on a hobby project I'm going to play with F#.

Re:F# is gaining independence from .NET by pharmakom on Monday February 01, 2021 @03:29AM (#25986715)
It’s possible but it’s just not the same.
Re:F# is gaining independence from .NET by andi999 on Monday February 01, 2021 @03:17AM (#25986654)
I think one problem is (was?) winforms is not available in f#. (which is c# unique selling point i. m. h. o.)
Re:F# is gaining independence from .NET by beyon on Monday February 01, 2021 @03:48AM (#25986822)
Are you thinking of something else than winforms?

I believe winforms and WPF has always been usable but not with the visual designer tooling in visual studio.

UWP has been trickier though to my knowledge.

Re:F# is gaining independence from .NET by andi999 on Tuesday February 02, 2021 @03:40AM (#25998605)
I probaby meant the visual designer tooling. (Which is a big point in using Visual studio)