News for hackers. Stuff that matters.
Re:Official proposal for Type Unions in C# by tombert on Wednesday August 07, 2024 @01:54PM (#41183686)
Huh, I did F# for years with discriminated unions, and I guess I just assumed C# would have had them by now.

I know not everyone likes them, but for typed languages I find it extremely hard to go back to languages without ADTs of some kind. I do Java for my current job, and Java is generally fine enough, but it's a little annoying when I have to do whacky workarounds with wrapper classes to get something that would be done in three lines of F#.

Re:Official proposal for Type Unions in C# by Eji1700 on Wednesday August 07, 2024 @01:56PM (#41183711)
F# is so hard to walk back from. I wish Microsoft would support it better and actually push it, because it's such a perfect sweet spot. Most of the functional advantages without being shackled to pure functions and the like is so easy to develop in.

Instead they've been, very slowly, turning C# into F#, which is even weirder to watch.

Re:Official proposal for Type Unions in C# by tombert on Wednesday August 07, 2024 @02:01PM (#41183743)
I agree; F# is probably my favorite of the "compromise" functional languages [1], where you can drop into the "wrong" way when necessary. F# pushes a more or less pure approach, but in some cases, like when mutation will be a bit easier and/or faster, it's easy to do that as well. I also think that even F#'s OOP is actually really pleasant...If nothing else, it's a lot more terse than C#'s.

I miss writing it; I haven't had a job using it in a few years but I really enjoyed writing code in it when I did. Most of my personal projects haven't really been able to use .NET for awhile, so I never seem to have an excuse to play with F# in my personal time.

I never got into C#, so I can't say that I really feel the pain of the C# transition into F#.

[1] At least in the typed world. I'm also really partial to Clojure.

Re:Official proposal for Type Unions in C# by kriiuuu on Wednesday August 07, 2024 @02:02PM (#41183758)
I feel the same about Scala. I use Scala3 daily and almost every other language is such a step back. I have looked at F# and it looks like one of the only other languages I would enjoy as much as Scala. Haskell is nice too, but the ecosystem is just not quite there. F# being able to tap into the rich C# ecosystem and Scala being able to tap into the Java ecosystem is such a win and makes them feel a lot less niche when you use them.
Re:Official proposal for Type Unions in C# by tombert on Wednesday August 07, 2024 @02:07PM (#41183804)
I do like Scala, and I haven't touched Scala3, but I've found it pretty unwieldy for actual use. It feels like I'm constantly fighting with the type system in a way that ends up being annoying an unpleasant.

To be clear, it's not like I'm new to functional languages and their type systems, I've been doing Haskell for forever, and I did F# for a multiple years, but Scala has never really clicked for me.

Personally, for doing JVM stuff, I'm a pretty die-hard Clojure advocate. I feel like it's easy for me to write Clojure in the way that I think, while still allowing me to mooch off the entire Java ecosystem, and if there are things that I don't like about the language it's really not that hard to macro in a new feature (though obviously this should be used sparingly).

I definitely recommend giving F# a try. I think it's an extremely underrated language.

Re:Official proposal for Type Unions in C# by agent281 on Wednesday August 07, 2024 @03:03PM (#41184292)
> I definitely recommend giving F# a try. I think it's an extremely underrated language.

Do you have any recommendations for people who do not have C#/.NET experience who want to rip their toes in F#? The last time I tried the language I bumped my head on the .NET parts. :/

Re:Official proposal for Type Unions in C# by tombert on Wednesday August 07, 2024 @03:23PM (#41184456)
Out of curiosity, what confused you about the .NET stuff? I didn't have any .NET experience going into F# either [1] and I didn't find it too hard to pick up the .NET stuff.

For the most part I didn't need most of the built in .NET libraries; the F# stuff was fine, but the ones I ended up using a lot were the threadsafe libraries for when I needed mutation across threads. ConcurrentDictionary was one I used an awful lot, ConcurrentBag occasionally, SemaphorSlim, and Interlocked Incrementors.

The reason that the .NET compatibility was handy to me was because it made it easy to get third party libraries without much fuss, which was great because they were generally pretty well supported.

The rule of thumb that I used was that F#, while it kind of looks like Haskell, is not Haskell, and specifically it's not lazy, and as such I always kind of pretended I was in the `do` block. Things execute top down, and you can mix in side effects wherever, so you kind of have to pretend everything is in an IO monad, or at least if you see anything that has a side effect in there.

[1] I was hired at Jet.com specifically because I had Haskell and Erlang experience prior. I had never written any significant C# code before.

Re:Official proposal for Type Unions in C# by agent281 on Wednesday August 07, 2024 @03:52PM (#41184723)
I was working on a Linux laptop and the installation process wasn't immediately clear. Some of the frameworks I looked at didn't target the most recent .NET version so I had to install multiple versions. Once I had it installed, I had compilation failures with a project that was built on Windows. The team that ran it wasn't sure what was happening.

Also, having the documentation split between the F# parts on one site and the .NET parts on another site was a bit of a pain.

It might have gone better if I was starting with a book instead of picking docs off the web.

Re:Official proposal for Type Unions in C# by tombert on Wednesday August 07, 2024 @03:55PM (#41184753)
Ah, yeah, the non-windows stuff was pretty clunky for awhile, but I do feel like it's gotten a bit better at the command line support.

FWIW I think it works fine with Nix, so if you're not opposed to using that package manager it should pretty trivial to get different versions working with a nix shell and/or flake.

Re:Official proposal for Type Unions in C# by progmetaldev on Wednesday August 07, 2024 @05:25PM (#41185723)
Hopefully this website can get you further with the installation. There will probably still be issues between Windows and Linux specific tutorials, depending on what you are trying to do.

https://fsharp.org/use/linux/

Re:Official proposal for Type Unions in C# by pjmlp on Wednesday August 07, 2024 @04:12PM (#41184914)
It is a dream learning guest languages, while trying to avoid the platform, one really needs to understand UNIX, Web, .NET, Java, Erlang... for using whatever alternative leaky abstractions are provided on top of them.

In F#'s case, there are plenty of good books, and then it is really needed to learn the platform.

Some well known ones => Stylish F#, F# in Action, Domain Modeling Made Functional

Re:Official proposal for Type Unions in C# by progmetaldev on Wednesday August 07, 2024 @05:27PM (#41185751)
Thank you for the recommendations. I still find using a physical book easier to learn from, even if they do tend to be out of date faster than online resources. I feel as though F# doesn't move as fast as other parts of the .NET ecosystem, and in particular the Domain Modeling Made Functional seems like it will be relevant for years to come.
Re:Official proposal for Type Unions in C# by agent281 on Wednesday August 07, 2024 @09:40PM (#41187291)
It's not so much that I don't want to learn the platform. It's that there is often an inherent sequencing. The expectation is you already know the platform and now you are learning the guest language. I just want to be taught the guest language and the platform at the same time.
Re:Official proposal for Type Unions in C# by progmetaldev on Wednesday August 07, 2024 @05:22PM (#41185686)
While a bit dated, F# For Fun and Profit is a great learning resource:

https://fsharpforfunandprofit.com/

Re:Official proposal for Type Unions in C# by jmaker on Thursday August 08, 2024 @01:03AM (#41188279)
My experience is diagonally opposite. I haven’t written Scala for a while now, I guess a year or two by now, and just a few days ago got back to it because the logic I wanted to express was hard to fit in to Java, Kotlin, C#, Go. I have certain performance and correctness requirements and most stuff is JVM based. So I thought like okay, I can isolate it in a service and invoke from another JVM, but let me just try to express my type families and the algos the easy way around, and whoosh I see Scala compile and give me the correct results on my first attempt.

That was a bit of a surprise - I’d forgotten how expressive Scala is. I didn’t need any “advanced” type system features. But my time to market was literally a couple hours instead of days like with C#, Java, Kotlin, Go, bending to fit the logic into the confines of their type systems. Kotlin is just awesome and very expressive. But it turns out, to me, Scala is somehow very natural - just write what you think, and it works.

Interestingly, that was also my experience many years ago when I first exposed myself to Scala in a numerical analysis class just to avoid Java, and it just worked right away. I recall well that surprised feeling. So I’m kinda happy to move back to Scala now. But I’ll also try to implement the same in F#, right after delivering the next feature set.

Re:Official proposal for Type Unions in C# by orthoxerox on Friday August 09, 2024 @03:47AM (#41199647)
When I have to use Scala, I am always confused by the amount of magic that is happening behind the scenes. It's so much magic that even the best IDEs become confused with me. C# is smarter in that regard: 99% of useful syntax is method calls, and typing the dot doesn't drown you in overly generic suggestions.

From what I've seen, Scala3 is better in this regard (at least the magic part), but I since Spark doesn't support it yet, my exposure to it has been limited.

Re:Official proposal for Type Unions in C# by pjmlp on Friday August 09, 2024 @10:05AM (#41201955)
There is enough magic with attributes, code generators, dynamic, reflection and expression trees.

Thing is the community doesn't reach that often to them.

Re:Official proposal for Type Unions in C# by electroly on Wednesday August 07, 2024 @02:30PM (#41184001)
I'm glad they're simply turning C# into F#. They're coming to us where we are, rather than forcing us to make a jump. I'm a relatively "blue collar" developer, I know a little about FP (not enough to be productive in F#) but I have tons of C# experience. I appreciate them taking the good stuff from F# and making it understandable to C# developers. It's been a delightful progression; no big jumps, no paradigm shifts, just the steady addition of useful new features that I can understand.
Re:Official proposal for Type Unions in C# by Eji1700 on Wednesday August 07, 2024 @02:41PM (#41184085)
So the issue for those of us in F# land, is that it's really not a big jump from C#. I've taught it (lightly) to at least one C# dev who basically picked it up in a weekend.

Something that helps, a ton, is as you're transitioning, you can write code almost identically to how you did before. It doesn't force FP on you at all. It's less useful if you try to do that, because you're not taking advantage of the language, but it makes testing your beliefs on how things work SUPER easy because you can set it up in the OOP way, get the result you expect, then set up in what you think is the correct FP way, and see if it matches.

The only issue is there's a severe lack of beginner friendly documentation (although it's leagues better than it used to be). I honestly think that if you're a C# dev, it's probably worth giving it a weekend or two to play with. ESPECIALLY if you like linq syntax.

Re:Official proposal for Type Unions in C# by progmetaldev on Wednesday August 07, 2024 @05:32PM (#41185803)
I agree with you, as far as adding new features to C# from F#. When I have the time to refactor larger codebases, I'm often finding new C# features/syntax makes the code easier to understand and reason about (often due to decreasing the number of lines needed to accomplish the same thing, without overcomplicating things). I do make sure that if I'm introducing new language features, that I'm refactoring related portions of code, so I'm not leaving behind the "old" way of doing things while also introducing the "new" way for the same functionality. I'm always thinking of the next developer with anger issues coming into the codebase after me.
Re:Official proposal for Type Unions in C# by freilanzer on Thursday August 08, 2024 @06:03AM (#41189805)
> I'm glad they're simply turning C# into F#.

I'm not. I find F#s syntax much more pleasing and elegant, and I'm afraid F# will wither away when C# is a watered down version of it.

Re:Official proposal for Type Unions in C# by ctenb on Wednesday August 07, 2024 @01:58PM (#41183720)
It makes sense from a language adoption standpoint, especially if you look at typescript for example, which is also by Microsoft. Though I agree with you personally
Re:Official proposal for Type Unions in C# by kbgart on Wednesday August 07, 2024 @07:50PM (#41186693)
Looking at the syntax in the type union proposal, I hope they take a bit more from F#. It really needs some work.
Re:Official proposal for Type Unions in C# by ctenb on Thursday August 08, 2024 @08:00AM (#41190554)
What are some issues you see with the current proposal?
Re:Official proposal for Type Unions in C# by kriiuuu on Wednesday August 07, 2024 @01:59PM (#41183727)
Good news is that you can achieve this with relatively little boilerplate in Java with sealed interfaces and records now. Relative to Java that is
Re:Official proposal for Type Unions in C# by tombert on Wednesday August 07, 2024 @02:02PM (#41183755)
Yeah, I know, and I have started using those, though that doesn't undo the last 15 years of Java code that I've written where that wasn't really an option.

Still, good to see Java joining the 21st century at least.

Re:Official proposal for Type Unions in C# by cies on Thursday August 08, 2024 @04:21AM (#41189287)
> that doesn't undo the last 15 years of Java code that I've written where that wasn't really an option.

Same for C# with this new feature. Sum types (as I like to call tagged/type unions) are such an important tool to have, not having it makes languages resort to all kinds of abominations, e.g. exceptions, product types (= records, structs, etc) that are actually sum types, "null to signify something went wrong", etc.

Not having them from a language's start results in an std lib that does not use them and hence teaches new comer to the language a bad way to do it.

Re:Official proposal for Type Unions in C# by karmakaze on Wednesday August 07, 2024 @07:48PM (#41186691)
Java can't do the ad-hoc mixing of types from different libraries, which is something I've wanted/needed for some ORM+expressions work I was doing. I really like where C# is going with this. Hopefully it will accelerate continuation of the evolution of Java.

I'm almost of the mind to use F# or C# instead of waiting.

Re:Official proposal for Type Unions in C# by bad_user on Wednesday August 07, 2024 @09:58PM (#41187406)
Those are untagged union types and are less useful than you think.

For one, they don't work for generic code where you want to discriminate. I.e., if you have a generic A or B type, you can't pattern match on it. And it's hard to add restrictions to the generic types such that it would work.

Such types sort of work in TypeScript, but TypeScript also has structural typing, meaning that it's designed for it.

Re:Official proposal for Type Unions in C# by Arnavion on Wednesday August 07, 2024 @05:29PM (#41185770)
As of 2020 (when I stopped using F# and .Net in general), the CLR only had inheritance and F#'s enums were actually implemented using inheritance. Eg Some and None were derived classes of Option class. You would see it if you inspected an F# assembly in a decompiler. I don't know if it's still the same today. This C# proposal has anonymous enums using `A or B` syntax which would be hard to make work as sugar over inheritance, so I guess the CLR would support enums first-class for this to work (if it doesn't already).
Re:Official proposal for Type Unions in C# by tkubacki on Wednesday August 07, 2024 @10:54PM (#41187688)
what's is your .NET replacement ?
Re:Official proposal for Type Unions in C# by Lanayx on Thursday August 08, 2024 @06:45AM (#41190009)
This is only partially true. Both Some and None were always instances of single FSharpOption class (None is actually not an instance, but null), however custom DUs are indeed compiled into classes with inheritance
Re:Official proposal for Type Unions in C# by Arnavion on Thursday August 08, 2024 @01:24PM (#41194023)
Ah, then I misremembered.
Re:Official proposal for Type Unions in C# by bad_user on Wednesday August 07, 2024 @09:51PM (#41187363)
FYI, Java has tagged union types (since version 16).

Pattern matching too.

Re:Official proposal for Type Unions in C# by sideeffffect on Thursday August 08, 2024 @07:57AM (#41190533)
> Java is generally fine enough, but it's a little annoying when I have to do whacky workarounds with wrapper classes to get something that would be done in three lines

This is a weird take, given that Java has had ADTs for years

Records https://en.wikipedia.org/wiki/Java_version_history#Java_16

Sealed classes https://en.wikipedia.org/wiki/Java_version_history#Java_17

And pattern matching for almost a year https://en.wikipedia.org/wiki/Java_version_history#Java_21

Otherwise big agree on how delightful to work with F# is.

Re:Official proposal for Type Unions in C# by tombert on Thursday August 08, 2024 @10:00AM (#41191628)
Most of my jobs until recently have been using Java 11, so I didn’t get to use these things.
Re:Official proposal for Type Unions in C# by sideeffffect on Thursday August 08, 2024 @11:18AM (#41192457)
I see. But no hope is lost! You can always lean on Scala. It runs fine on any Java >= 8 and it's great. Unless you intentionally used is badly (but you could say that about any language).
Re:Official proposal for Type Unions in C# by johnzabroski on Friday August 09, 2024 @06:20AM (#41200451)
Hmm. You find it hard to go back to languages without ADTs yet you use Java for your day job. Okay.