Patterns to mix F# and C# in the same solution

前端 未结 8 781

I studied few functional languages, mostly for academical purposes. Nevertheless, when I have to project a client-server application I always start adopting a Domain Driven

相关标签:
8条回答
  • 2020-12-24 12:03

    I've written a WCF service in F# that acts as a translator plug-in for reading a WFS (geospatial data) service. The code turned out nice and concise.

    While the standalone dll I compiled worked fine inside my colleague's C# solution, he did try to strangle me when I showed him the code. Culture shock, I think.

    So did we use F# and C# in the same project? Yes and no. No, because I rewrote the thing in C#. Yes, because building and testing the prototype in F# saved me more time than it took me to translate it to C# LINQ-style.

    I wouldn't want to try building everything in F#, but I'm patiently waiting for the day I can work on the data processing/algorithmic part of a mixed language solution in F# without fearing for my life.

    0 讨论(0)
  • 2020-12-24 12:04

    Have not tried it yet, but one place where I sure would like to use functional language, even though this might not be a reason to use it, is so that I can avoid what has been describe here

    http://steve-yegge.blogspot.com/2006/03/execution-in-kingdom-of-nouns.html

    I would like to just name this as a verb, and execute it

    calculateSomething() instead of SomtingCalculator.Execute()

    0 讨论(0)
  • 2020-12-24 12:07

    You got me thinking, and I tried to decide where I would do it. There are two situations that spring to mind:

    1. If I am making a castle (MVC) project I would probably have controllers in C# while all the BL and models are in F# (I tend to do domain driven design and wire the BL into the models, or via injected components [ala DI])
    2. Starting a new project but incorporating existing libraries so as to not reinvent the wheel.

    Further, I'm a big advocate of 'right tool for the job', so if I think one or the other would suit better, I'd use that.

    0 讨论(0)
  • 2020-12-24 12:07

    Yes, in future I think that we must combine these kinds of languages like OOP (C#) with Functional language (F#) to take advantage of multicore processing. C# 4 has classes to support this but some things are done better in F#.

    0 讨论(0)
  • 2020-12-24 12:08

    There are certain places where traditional functional techniques make a lot of sense, and lead to code that is both smaller and more concise. A classic example is text parsing and tree processing, both often appearing together when you're implementing a DSL. F# features like anonymous iterators, extensible pattern matching, and ability to define custom infix operators to serve as combinators really helps a lot here. Meanwhile, on the C# side, LINQ is a good start, but it doesn't take you all the way there.

    I suggest you have a look at FParsec, and see for yourself how much better suited it is to advanced text processing / parsing than any library you could possibly write in C#.

    0 讨论(0)
  • 2020-12-24 12:10

    Languages are just a tool. Like Luke, I'm a big fan of using the right tool for the job. If a particular app would benefit from using both C# and F#, then mixing seems reasonable to me.

    As for how to do it, see:

    Can you mix .net languages within a single project?

    Bottom line: you can merge two DLLs that use different languages into a single DLL as a post-build step. Or, you can use multiple languages in a web site that's compiled on the server-side.

    0 讨论(0)
提交回复
热议问题