5

I have a Java class and I need to write a similar one in F#. How can I define a private class inside public in f#?

public class KnownRuleGoals {
    void record() {
      knownParsingSet.add(new KnownParsing());
    }

    private class KnownParsing {
        Rule [] knownParsing;
        KnownParsing() {
         ...
        }
        void doWork() { ... }
    }
}
9
  • 1
    F# does not support class nesting.
    – vcsjones
    Jan 25, 2016 at 21:56
  • 2
    In functional programming one thinks functions first, in OO one thinks objects first. You are thinking object when you should be thinking function. Think how do I create a public function record() that has a hidden function KnownParsing(). A function inside of another function is essentially private.
    – Guy Coder
    Jan 25, 2016 at 22:25
  • 3
    Why all the down-votes? This sounds like a reasonable question.
    – Mathias
    Jan 26, 2016 at 0:26
  • I don't think a question like this should be downvoted either. Have an upvote!
    – Grundoon
    Jan 26, 2016 at 0:33
  • I haven't voted down (and don't plan to) but I do agree with @GuyCoder that this is not an idiomatic way of writing F#. What I would like to see is further clarification on intended use from the questioner so that we can suggest an alternative solution which is idiomatic. Jan 26, 2016 at 0:35

2 Answers 2

7

It is perfectly possible in F#. F# is functional first, not pure functional - and this is one of its major strengths. F# is pragmatic and this construct below lets one achieve exactly the same result, even though the KnownParsing type is not "nested" in Java or C# sense.

type KnownRuleGoals() =
    let knownParsingSet : List<KnownParsing> = List()
    member this.Record() =
      knownParsingSet.Add(new KnownParsing())

and private KnownParsing() =
    let foo = "private fields"
    member this.DoWork() = failwith "TODO implementation"

Object expressions are useful for implementing interfaces or abstract classes, but the "parent" class (the one that creates an object expression) won't be able to access its internals. Or, more correctly, there is no way to create internals other than interface/abstract members in any way other than via ref cells outside the object expression itself. This has performance implications such as GC pressure for ref cells.

See this example in my open-source project about how object expressions and "nested via and" types work interchangeably for IEnumerator implementation. On this line I use private type as in this answer and it works well. In the whole project I use F# in mostly imperative way because this is the only way to get decent performance. And I must say I like F# most often over C# even for imperative code.

1
5

As people have said, F# does not currently support nested classes.

One workaround is as shown by V.B., with two separate classes, one of which is private.

However, if the inner class is simple (with one or two methods only) then there is an alternative approach, which is to use a closure as a poor-person's object.

In the code below, the KnownParsing function creates two functions and returns them as a pair. The knownParsingSet then contains pairs of functions, rather than instances of classes.

If there is only one method in the nested class, then this approach is fine because a one method class is basically just a function. For more than one method in the nested class, it gets quite ugly, which is why F# people solve the problem using other techniques instead. :)

type KnownRuleGoals() = 

    let knownParsingSet = System.Collections.Generic.HashSet()

    // use a function
    let KnownParsing() =
        let knownParsing = [||]
        let doWork() =
            ()  // do something
        let doWork2() =
            ()  // do something
        // return the pair of functions
        doWork,doWork2

    // the member/method comes after the closure because
    // definitions must come before usage.
    member this.record() = 
        knownParsingSet.Add(KnownParsing())
11
  • Additional allocations just to be pure - tuple + 2 Funcs at least, with no hope for inlining by Jit. Let me just understand why so many F# users are so against much more simple solutions in favor of purism? That question haunts me for 2+ years.
    – V.B.
    Jan 26, 2016 at 0:57
  • The question was not about performance, just on how to implement nested classes. I certainly wouldn't claim that this is a optimal solution! I was just showing an alternative solution for the OP.
    – Grundoon
    Jan 26, 2016 at 1:00
  • @V.B. For me it is that functional code is more likely to be correct when it is purely functions. If other pure functional languages like ML became more main stream and had better tools and support I would be using them instead. If Coq could be used as a programming language I would use that. If Mercury were more mainstream and had better tools I would use that. I also rely on Algebraic Data Types and so despise if statements
    – Guy Coder
    Jan 26, 2016 at 1:01
  • @GuyCoder that is at least an answer with a reasoning behind, thank you! My experience with F# (in my domain) has taught me that much stricter type inference (e.g. to C#, it has some) and lack of implicit conversions is enough in most cases. But there is another approach I try to adhere - use mutable state when it matters for performance and keep it private. Best of the two worlds.
    – V.B.
    Jan 26, 2016 at 1:06
  • @V.B. I also forgot to mention F* which is F# with an added proof system. If this goes mainstream with Visual Studio, I will be seeing F# only in the back window.
    – Guy Coder
    Jan 26, 2016 at 1:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.