11 ms·
A whirlwind tour of object-oriented code in F# (2012)
- holtalanm 7y agothis is really interesting. I've been meaning to dig my teeth into F# sometime soon because of its highly functional base.
- melling 7y agoHere’s a Tour of F# by Microsoft: https://docs.microsoft.com/en-us/dotnet/fsharp/tour https://docs.microsoft.com/en-us/dotnet/fsharp/tour
- adgasf 7y agoF# is a joy to use. After a certain time, many C# developers find themselves writing in a functional style. F# makes this the default, but lets you continue using all of your existing code. Better yet, Linux and macOS support via .Net Core is excellent. F# on .Net Core is now a powerful alternative to Node.js.
- kemiller2002 7y agoThe thing I find most interesting about a lot of C# developers I've worked with, trained, etc. is their understanding of "Object Oriented" programming. I have the same discussion over and over again. "Have you looked at F# even to just to see how it works?" "I won't ever use it. I'm an OO programmer, and it suits me just fine!" "Do you use LINQ, and understand you can pass functions into methods etc.?" "Yeah, it makes things so much easier. I don't understand why people wouldn't use it." "That's really more like functional programming" "...." Edit:formatting
- bunderbunder 7y agoIt's almost to the point where the deciding factors in choosing between (C#-style) OOP and (F#-style) FP are: - Do you want your data types in one line or 20? - Pattern matching, or large concretions of if-statements? - Race conditions: Quality problem, or fun puzzle?
- mfatica 7y agoC# has most of the same pattern matching functionality at this point
- McWobbleston 7y agoThere's no compiler warning for incomplete matches, and without discriminated unions it's pretty verbose to write pattern-match friendly types C# has support for most F# features, they're just often awkward to use
- macca321 7y agoYou can get a pretty reasonable facsimile using some libraries I made: Discriminated unions: https://github.com/mcintyre321/OneOf https://github.com/mcintyre321/OneOf and One line value objects: https://github.com/mcintyre321/ValueOf https://github.com/mcintyre321/ValueOf
- McWobbleston 7y agoI'm constantly impressed by ways people manage to extend C# like this. I'll have to try this out :)
- coldacid 7y agoI haven't had a need for ValueOf yet, but OneOf has saved my ass now more times than I can count. Thank you so very much for it!
- seanmcdirmid 7y agoThere are C# programmers like that I suppose. There are also many C# programmers who buy into multi paradigmism, they fee no ideological conflict between using OO or FP as needed. C# supports FP well enough, and gets better every release (eg pattern matching). It’s difficult to compete with C#, especially when F# is feeding features into it.
- dvlsg 7y agoHopefully F# feeds the ability to have functions at the root of a module into C#. It's a small thing, but needing to put pure functions on static classes gets old after a while. I know local functions are a recent ish addition to C#. Hoping they keep going down that road.
- seanmcdirmid 7y agoC# has a set of design constraints that can be annoying, but they make the language consistent and extremely resilient. It isn't up to the whimsies of the designers either, Anders and company went one way with C# and a completely different way with TypeScript (given its need to fit in with Javascript). They can be annoying, I guess, but once you are used to it you don't really notice they are missing. It is annoying if you have to switch languages often, I'm pretty sure I would need a few weeks to get re-acclimated if I had to go back to C# from using TypeScript for so long.
- onemoresoop 7y agoNode has its place, i wouldn’t call F# an alternative to it. The node community is absolutely fantastic. F# is also an amazingly powerful language and im happy using it on the back end
- smt88 7y agoWhat is Node's place?
- RhodesianHunter 7y agoThe first stepping stone the front-end folks take into server side programming. :D
- sdegutis 7y agoWhat would you choose instead if you needed to write a simple web server?
- McWobbleston 7y agoF# with Giraffe is super simple to get up and running and it fits into the rest or the ASP.NET Core ecosystem
- smt88 7y agoSimple web servers are already written for me. If I'm serving static files, I'll just use a CLI tool or S3. If I'm making a simple API, I've already moved into territory where I'd prefer to use C# or something like that. The ecosystem is better and you can write it to avoid runtime gotchas. Big languages/stacks can still write tiny programs.
- sdegutis 7y agoConsidering we're in an F# thread, it makes sense that both alternatives to Node.js mentioned were part of the Microsoft stack (C# and F#). This furthers my conclusion that people just prefer the tools they're already familiar with, for better or worse.
- louthy 7y ago> After a certain time, many C# developers find themselves writing in a functional style If you can't move to F# or find its tooling abysmal and slow then I have a library [1] that makes the inertia flow positively in the functional direction in C# https://github.com/louthy/language-ext/ https://github.com/louthy/language-ext/
- winkelwagen 7y agoAfter learning f# I had to go back to c#. You library made it bearable. Also the reader.md and some of your more hidden articles are amazing. Thank you, and the other contributors so much, amazing work. My current strategy is to hook my colleges in with languag ext until they move to f# themselves.
- Dayshine 7y agoHah, the internet is such a small place. I thought that library looked familiar, and realised I was looking at it earlier today when I set up some F# job filters on stackoverflow (you're the only F# job on there that doesn't involve moving to London).
- Scramblejams 7y agoWould love to use F# with Unity, but the typical high allocation rates that come with the functional language territory scare me. Looked at your library, it's very interesting. I saw your docs note about the allocation efficiency of Map, would like to hear more. Could you characterize the library's general allocation story?
- louthy 7y agoI've done a short write-up about this: https://github.com/louthy/language-ext/wiki/Performance https://github.com/louthy/language-ext/wiki/Performance
- Scramblejams 7y agoTerrific info, thanks! Looking forward to using it.
- jeswin 7y agoC# for the most part does a decent job with Functional Programming, but it still leans very heavily towards statements instead of expressions. Even the newly introduced pattern patching (a common FP idiom) ended being a switch statement - perplexing decision because expressions are so central to good FP code. And if you were in imperative land, you didn't really need pattern matching so much in the first place; most developers will be fine writing if conditions. I hope they fix these warts. It can soon be a good alternative to Go for real native applications.
- svick 7y agoC# 8.0 will have a switch expression: https://docs.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-8#switch-expressions https://docs.microsoft.com/en-us/dotnet/csharp/whats-new/csh....
- adgasf 7y agoAs C# gradually becomes F#, I have to wonder, why not just use F#?
- runevault 7y agoYou aren't wrong, but a lot of people are only comfortable in one language/style of language (so either just say Java/C#/c++ or maybe all Algol derived languages). Growing beyond that can be scary until you've done it a couple times if you wait until later in your career to start trying things like say Lisp or Haskell.
- louthy 7y agoAh, the same old refrain [1] [1] https://github.com/louthy/language-ext/wiki/%22Why-don't-you-just-use-F%23%3F%22 https://github.com/louthy/language-ext/wiki/%22Why-don't-you...
- dfgdghdf 7y agoNice article, thanks for sharing. I'm sure there are people out there debating F# vs C# for a new project, and might not realize how many future C# features are already in F# though.
- mikece 7y agoWhile there's nothing you can do in C# that cannot be done in F#, the reverse is not true. The biggest thing that comes to mind is to influence the direction of the other language (C#). I've heard it said many times that if you want to see what features will be in C# in the future, look at F#. Too many features to cite (though Linq is the biggest that comes to mind) were in production use in F# long before they arrived in C#.
- McWobbleston 7y agoThere's actually a couple of minor things (like covariance). I was surprised to learn F# doesn't support creating sealed methods on classes the other week when I was extending some OO code. Thankfully F# and C# mix so well together I could've written a quick C# project to do it if needed, but I didn't want to break up my F# project into two + some C# glue In the vast majority of cases though what you said is true. Discriminated unions, records, computation expressions, and type providers are total game changers
- svick 7y ago> While there's nothing you can do in C# that cannot be done in F# Really? When did F# start supporting pointers? > to influence the direction of the other language (C#) Lately, it seems it's the other way around: C# is introducing features like Span<T> or default interface members and F# is (quickly) catching up.
- runevault 7y agoTo your last point, with the freedom they seem to feel after the Core breakage (like Span/Memory) C# might start innovating on its own again instead of just taking awesome pieces from f# and making them part of C#.
- sleibrock 7y agoYears ago I was working a miserable job writing boilerplate template code where I had to sub in variables based on certain conditions, it was using a very bland language to look up data from our database and format the output. I wasn't allowed to download any software onto my work computer despite having a technical background. So what I did instead to make my life easier was write F# to create a kind of DSL and create the template lookup scripts for me. I did it all from the browser with repl.it, and I generated dozens of scripts and saved myself a lot of time in the process. Now I mostly use Racket, but F# is where I kind of started out. It's a fun dialect.
- nhlx2 7y agoReads like an opening of a dystopian novel!
- mfatica 7y agoYour idea of dystopia is a company having strict IT policies? Wait til you hear what China is doing.
- oblio 7y agoHeh. I was doing manual deployments because of dumb bank policies so after a lot of maneuvering I used Powershell and plink.exe to write a small, dumb version of Python's Fabric to be able to automate deployments. I was writing Powershell in Vim while trying to keep my skunkworks project out of sight because of crappy management policies. Sometimes you gotta make lemonade...
- escherize 7y agoWhat do you do where you are using Racket most of the time?
- sleibrock 7y agoFor now it's a lot of database ETL and network services at my new job. If I'm not using Python, there's a good chance I'm prototyping something in Racket.
- GiorgioG 7y agoThe problem is F# doesn't get enough resources internally at Microsoft. How many times has VS shipped missing the latest version of F# tools? In terms of community, it's tiny. If I want to do functional programming, F#/.NET isn't the strongest option. I have several books on F# and as much as I'd like to learn/use it beyond toying around, I can't help but feel like all it's worth using for is tinkering (outside of the few shops that use F# extensively.)
- bunderbunder 7y agoI find this to be an interesting cultural phenomenon, this thing where people in .NET-land are skittish about F# because it's not so tightly controlled by Microsoft. Meanwhile, I'm pretty sure nobody even wants Oracle to be getting involved in Kotlin or Scala or Clojure.
- GiorgioG 7y ago> this thing where people in .NET-land are skittish about F# because it's not so tightly controlled by Microsoft. Microsoft's great tooling is what has made (traditionally) it sticky for a lot of developers. F#'s tooling from Microsoft has been a half-hearted effort (organizationally, not speaking of the effort that the F# guys have put in.)
- Rapzid 7y agoThe irony is that F#s releases are in lock-step with the Visual tools team releases and schedule.
- GiorgioG 7y agoI must vehemently disagree. From: https://github.com/dotnet/fsharp/issues/2400 https://github.com/dotnet/fsharp/issues/2400 "GiorgioG commented on Oct 19, 2017 @masaeedu - Not to beat a dead horse, but we've been waiting for an RTM release of this since May when VS2017 first RTM'd. We were then told it would likely come in the July release of VS, we're now nearing the tail end of October with no clear idea when we'll get it. It's not @cartermp 's fault, just the reality that MS has not made it a high priority (judging by their actions, not their words.) In summary, if you want to use F#, use VSCode, it has excellent support for F#, as long as you realize it's not Visual Studio, it's a souped up text editor."
- shitgoose 7y agoOO in F#. there goes the language...
- onemoresoop 7y agoNot true. You actually are not to use OO in F# unless you really have to and there are certain cases where it helps
- nbevans 7y agoF# is a pragmatic dual paradigm language where both OO and FP are fully supported and encouraged. There is no "[don't use OO in F# unless you really have to]" sentiment anywhere.
- cwyers 7y agoConsidering that the article is from 2012, it "went" a long time ago.
- adgasf 7y agoOOP is not idiomatic F#. It is mostly for interop with C#.
- exyi 7y agoI would not say that, sometimes it's quite powerful to combine these paradigms. I think they would not design a language for CLR only to force them to create a huge interop subsystem. You are right, however, that OOP should not be the default way to write things in F#.
- McWobbleston 7y agoI definitely appreciate being able to drop into an OOP style when I find it's necessary. In a talk Don Syme (F#'s original designer) goes into aspects of OO he enjoys, those understands the usefulness of, and those he does not enjoy. I believe it was F# Code I Love, which gives some good insight into the thinking behind the language.
- dmitryminkovsky 7y agoFor all the love Microsoft has been getting these days, I still don’t get how I’m supposed to write C# or F# without Visual Studio and Windows. Am I missing something? VSCode doesn’t support these languages, right? I’m a person who spent a long time trying to write Java in Vim, but switched to IntelliJ and wouldn’t want to write C#/F# without some comparable IDE.
- dustinmoris 7y agoVS Code supports both languages. Check out Ionide for F# support. You can also use JetBrains Rider. Visual Studio also works on macOs btw and if nothing satisfies your taste then you can always just use a plain text editor because all you need to build, run and test your code is provided by the dotnet CLI.
- lstamour 7y agoF# in VS Code: https://docs.microsoft.com/en-us/dotnet/fsharp/get-started/get-started-vscode https://docs.microsoft.com/en-us/dotnet/fsharp/get-started/g... F# in .NET CLI: https://docs.microsoft.com/en-us/dotnet/fsharp/get-started/get-started-command-line https://docs.microsoft.com/en-us/dotnet/fsharp/get-started/g... C# in VS Code: https://docs.microsoft.com/en-us/dotnet/core/tutorials/with-visual-studio-code https://docs.microsoft.com/en-us/dotnet/core/tutorials/with-... Supported Linux versions: https://docs.microsoft.com/en-us/dotnet/core/linux-prerequisites?tabs=netcore2x https://docs.microsoft.com/en-us/dotnet/core/linux-prerequis... Supported macOS versions: https://docs.microsoft.com/en-us/dotnet/core/macos-prerequisites?tabs=netcore2x https://docs.microsoft.com/en-us/dotnet/core/macos-prerequis... Rider's also an option: https://www.jetbrains.com/rider/ https://www.jetbrains.com/rider/
- dmitryminkovsky 7y agoThank you for these links. I will look at them! But I am wondering qualitatively, along the lines of whb07’s sibling answer: is the experience good? Is it as good as on Visual Studio?
- 7y ago
- rygxqpbsngav 7y agoBeen using F# on personal projects and C# at work. I noticed my C# coding gone high quality once I got into functional thinking. It is highly recommended for anyone to check/learn F#. F# is my goto lang. now to competitive programming too. I believe F# and ML will make a great combo, but python is still leading.
- danite 7y agoI used to be a big Haskell programmer. While I still love the language, I've really come around to the idea that strictly evaluated functional languages like F# or OCaml are the best for programming in. You get the nice functional features but don't have the straightjacket of laziness forcing you into certain design decisions. It's really nice sometimes to be able to mix in impure, side effectful code without having to thread it through a monad. The OCaml family feels like a nice blend of good sound functional features with enough escape hatches to be productive in real world programs that might occasionally need arrays or mutable state or IO. Laziness by default also makes things like debugging or reasoning about performance frustratingly difficult. I do miss Haskell's typeclasses in these languages, though. My ideal language is basically Ocaml/F# but with Haskell's syntax and typeclasses.
- adgasf 7y agoHopefully we will see this one day: https://github.com/fsharp/fslang-suggestions/issues/243 https://github.com/fsharp/fslang-suggestions/issues/243
- k_cieslak 7y agoHopefully not :-)
- jlturner 7y agoI use F# daily, and to get around this limitation I use hacky custom code generation an F# script (fsx) to generated specific types. Additionally, on cases where you’re not specifying the data of the type itself, you can use static type constraints on members. This doesn’t give you a default implementation like Haskell but you can always provide one as a function. Btw the monadic threading is still very useful, especially when mixing impure code and mutation (when appropriate). The async and result monads, in F# computation expression form, are particularly useful. For a good example of async + impure code, check out MailboxProcessor, which is part of the standard F# lib. Works similar to CSP/goroutine + channels/actor models; makes parallelized concurrency and message passing easy. You can also make pure mailboxes easily by recursively passing data forward, but sometimes you don’t want to for memory / gc & alloc reasons.
- lol768 7y agoIt's a pity the tooling is comparatively immature, with F# (or at least that was my experience last time I tried to develop with it). I think Rider is getting there, though. To go into more detail, the initial implementation in Rider was limited to not much more than syntax highlighting. Inspections were fairly limited, ASP.NET MVC integration was not as featureful. On the C# side, it's much more developed and refined. Within Rider, it's obviously been the main focus so I suppose it's only natural it's received more polish.
- asp_net 7y agoI've been developing F# in Rider for the last 4 weeks, and especially the last update made a huge step forward. It's stable, fast, the test-runner works perfectly, etc. It's definitely not at the same level as the C# tooling yet, but it's good enough to be productive. (I'm on macOS by the way, building Xamarin apps).
- lol768 7y agoGlad to hear it's improved!
- Nelkins 7y agoIf you want to try F#, here's the full F# compiler in the browser run using WebAssembly. Very fun to play with: https://tryfsharp.fsbolero.io/ https://tryfsharp.fsbolero.io/
- Nelkins 7y agoAnd it also compiles to Javascript: https://fable.io/repl/ https://fable.io/repl/ You can browse through some sample apps using the side bar on the left.
- onemoresoop 7y agoThanks for posting that. Fable is fantastic
- new4thaccount 7y agoI just bought Scott's book on Railway Oriented Domain Driven Design. So far so good. I am the domain expert though, so no revelation there. The modeling of state machines using types and functions is pretty revolutionary to me for where I'm currently at development skill wise. Everything just seems so clean and elegant.
- arunix 7y agoThe book is "Domain Modeling Made Functional" https://fsharpforfunandprofit.com/books/ https://fsharpforfunandprofit.com/books/
- pjmlp 7y agoI missed the examples with EF, WPF, Windows Forms, UWP, WCF.
- bob1029 7y agoI think several interesting points were brought up in this thread regarding the assumption of functional aspects presented in F# by C#. LINQ being the most popular example. I am currently lead to believe that a purely functional application development domain is potentially an overreach of theory in terms of building things that can interact with the real world in very complex and 'functionally-leaky' ways. My biased perspective as someone who hasn't really used F# very much is this: Perhaps an imperative language such as C# 8.0, with a few high-value functional features sprinkled in, is actually the best of both worlds when viewed through the lens of someone who has to interface with really weird business systems. I use imperative techniques for handling remote calls, exception handling, etc. Then, when I need to work with my internal business logic or models, I can use more functional techniques. Based on my own understanding and other comments presented here, C# does not seem to preclude the usage of functional approaches to solving problems if you have the discipline and experience to do so. C# also brings with it a host of other benefits in terms of tooling support and simply being able to find other developers who can understand your codebase.
- McWobbleston 7y agoI really encourage you to use F#, as someone who's done C# for a long time I find F# (while not perfect) does strike near to that ideal balance of FP / imperative you speak of. Writing functional code naturally is a breeze, while the language isn't going to punish you or feel awkward if you need to drop in some mutable state or OO programming
- tigershark 7y agoOn the contrary, you have a huge advantage to interface with the dirty world using F# thanks to exhaustive pattern matching and you can make everything type safe to render invalid state unrepresentable.
- macca321 7y agoOne line data types https://github.com/mcintyre321/ValueOf https://github.com/mcintyre321/ValueOf
- macca321 7y ago... and exhaustive matching: https://github.com/mcintyre321/OneOf https://github.com/mcintyre321/OneOf I'm not saying it's as good as writing F#, or that C# shouldn't include these features of course.
- joshsyn 7y agoI started F# lately and found it absolutely amazing. I come from heavy C# programming, and always find myself reaching out to have sane default like immutable types. But unfortunately there are still lot of libraries out there for F# which don't support .net core fully. Especially db access ones.
- rishav_sharan 7y agoUnlike most commentors my experience with F# hasn't been stellar. I feel the language itself is well designed and the community is great but there is just so much of it. Any source code that i try to go through is sprinkled with so many new keywords, so many different way of combining stuff, that there seems to be so many syntactical stuff going on. It is really making me struggle. It just feels like a language where the maintainers are going overboard with. And I am sure that once I understand it, I would love it, but the path to get there is so hard. Pretty much no tutorials or articles are out there and reading sources for small projects is not viable for me either due to the keyword noise and too many ways to do something. I did not feel anything like this when I taught myself python, ES6, lua, Crystal etc. F# just feels too big.
- BeetleB 7y ago>Pretty much no tutorials or articles are out there and reading sources for small projects is not viable for me either. The lack of a good online tutorial is a problem for me as well. F# For Fun and Profit is decent, but I've not found it a good substitute for a book/tutorial.
- hoelle 7y agoI agree, F# is hard to learn. It took me a few months of tinkering to be able to express myself in it. Good entry points: Don Syme - his "F# Code I Love" talk has been recorded a few times: https://www.youtube.com/watch?v=aw2BAxG3bdM https://www.youtube.com/watch?v=aw2BAxG3bdM. I recommend catching a few versions. Scott Wlaschin - His talks are also great: https://www.youtube.com/watch?v=srQt1NAHYC0 https://www.youtube.com/watch?v=srQt1NAHYC0. He also runs https://fsharpforfunandprofit.com/ https://fsharpforfunandprofit.com/ which is a treasure trove. And I found Isaac Abraham's book excellent and breezy to read: https://www.manning.com/books/get-programming-with-f-sharp https://www.manning.com/books/get-programming-with-f-sharp Now that I'm over the hump, I think F# code is far more readable than other languages I'm well versed in (C++/C#). Probably my favorite thing about F# is the strict top-down dependencies. You can open a project and read it in order, and understand it without having to hop around a bunch. And since most programs can be written in far fewer lines in F#, code written in it ends up feeling very small and economical.
- nlawalker 7y ago> Anything C# can do... Yes, yes, you can do anything in F#, but what should you do? My opinion is that that what F# is missing is a good set of idioms and a strongly opinionated set of guidelines. To my beginner's eyes, every F# codebase I've seen feels like it's written in a completely different language; it's like the opposite of Python's "one right way to do everything". I've heard that F# is great for domain-specific languages, but in most cases a DSL is the cardinal opposite of what I want. I want a common language that I can use to express a multitude of different concepts. It seems like all of the guidance out there about F# is about showing off features, being extremely clever, or using F# to teach functional concepts, not actually about writing useful applications.
- GordonS 7y agoAs a long time C# dev who has dabbled with F#, I kind of feel the same. I always find myself structuring appa like an OO app, which invariably results in something that feels wrong. For someone for which OO is so entrenched, it's really difficult to think differently - and there doesn't seem to be an idiomatic way to write F# code (not that I've found, anyway); every code base seems to take a different approach.