0

Is there a way in F# to declare a class, but not define it like in C# or C++ or Java, or pretty much any other language, so that we can avoid the use of the and keyword? I have two fairly big classes that reference eachother, and I don't want to put them both in the same file like this:

type Type1 =
    // definitions

and Type2 =
    //definitions
user3685285
  • 6,066
  • 13
  • 54
  • 95
  • 3
    AFAIK you are out of luck - of course the question is why you need two huge classes that references each other in the first place ;) – Random Dev Mar 09 '16 at 17:11
  • BTW: one way out could be to refactor out the needed parts into interfaces `Interface1` and `Interface2` - you can define these in one file and then implement them wherever you like – Random Dev Mar 09 '16 at 17:16
  • I am very temped to give this question a down vote because you have not even shown that you looked for a functional solution or even shown any code for that matter. – Guy Coder Mar 09 '16 at 17:19
  • @Guy Coder First of all, I showed that I have a solution, but am looking for a cleaner workaround. Given that there is none, it's reasonable to assume that I HAVE looked to no avail. Secondly, this is not a coding question. I'm not debugging specific code, but merely asking a design question which only requires the code shell I provided. Readers have everything they need to answer the question. – user3685285 Mar 09 '16 at 17:25
  • 1
    @user3685285 uhh.... you should not point out that this is not about coding because you are wrong here in this case ... – Random Dev Mar 09 '16 at 17:38
  • I was referring to the large classes. If you show them then we might see some other way around the problem. I don't think there is one, but I can't say for sure without seeing the actual code. – Guy Coder Mar 09 '16 at 17:38
  • @Carsten What am I wrong about? Not asking for design help. Not gonna show the classes. That wasn't my question. My question was merely if there was an easier way to do this other than type...and. I have already determined the design is to have the two classes reference each other. Whether or not that's the best design is kind of irrelevant. – user3685285 Mar 09 '16 at 17:49
  • @user3685285 then why answer Guy with "am looking for a cleaner workaround" and "merely asking a design question"? - anyway I'm out at this point – Random Dev Mar 09 '16 at 17:55
  • @Carsten ok bad choice of words there, but if you look at the question, I don't think I fit into any of these off-topic categories: http://stackoverflow.com/help/on-topic. I asked about a very specific programming mechanism with just the right amount of code to show the reader what I mean. In fact, I can find many other questions like mine that are not off topic. If I'm mistaken, let me know what rule I violated and I'll edit. – user3685285 Mar 09 '16 at 18:02
  • it's not about some rule - it just don't look like a good question or a good fit for SO to me - btw: only the close-vote is from me - the downvote is not - I honestly don't know what more to tell you than what I did in my first comment above – Random Dev Mar 09 '16 at 18:05
  • Ok, I disagree that this is a bad fit for SO, but whatever. – user3685285 Mar 09 '16 at 18:33
  • You might want to check this out. It seems related: http://stackoverflow.com/questions/2854145/problem-determining-how-to-order-f-types-due-to-circular-references – Ringil Mar 09 '16 at 20:16

1 Answers1

1

F# doesn't make it any easier to have circular references than that. I am bit confused by your statement that C# and Java allow you to "declare but not define" a class; these languages have no concept of forward declaration like C++ does, and the only reason C++ has that is to support its header/translation unit-based compilation model, which C# and Java thankfully don't have.

Anyway. To go further I suggest you read Cyclic dependencies are evil and perhaps factor out a common top-level type for your 2 classes so they don't need to reference each other. F# allows circular references, but try not to work against the language.

Asik
  • 21,506
  • 6
  • 72
  • 131