4 ms·
>Strict dependency order >In F#, all variables, functions, types and files can only depend on variables, functions, types and files defined earlier. The benefi
by AndriyKunitsyn 2y ago
>Strict dependency order
>In F#, all variables, functions, types and files can only depend on variables, functions, types and files defined earlier. The benefits of this are the fact that a circular dependency is not possible by default and extra clarity with “what depends on what”, which helps during code analysis and PR reviews
Ehm. So it's like C... with no forward declarations?
- veber-alex 2y agoI don't understand how this is called an advantage. Dealing with cycles in Python is a total pain full of cryptic error messages, while in Rust cycles are fine as all top level items "come to exist" at the same time so they can depend on each other without any issues, and it makes refactoring a breeze. Sounds like a compiler limitation touted as a feature.
- p4bl0 2y agoNo it's not the same thing as in C. What they try to explain but quite badly phrase is that F# (or OCaml, on which it is based) is lexically scoped, values of non local variables are captured in definitions (so they must exist at that time, but also and more importantly they cannot be changed later because of the immutability by default). In C you can have a function using a global variable, and change to this global variable will affect the function behavior.
- pjtr 2y agoThis seems fine within a file, but isn't this problematic across files? Now the file order is significant, so not only is the Visual Studio XML project file is an essential part of the language semantics, you also can't organize your files in subdirectories freely? Or did they fix that at some point? How does that scale to larger projects?
- neonsunset 2y agoMore expressive nature of F# means you don't have that many files. C# is luckily and finally moving into that direction too. There was no reason for "one file per class" policy anyway, but it was still widely adopted historically. Here's an example of a worst-case scenario (GUI frameworks and the extensions have notoriously huge amount of code): https://github.com/fsprojects/Avalonia.FuncUI/blob/master/src/Avalonia.FuncUI/Avalonia.FuncUI.fsproj#L31-L187 https://github.com/fsprojects/Avalonia.FuncUI/blob/master/sr... But realistically an average project would look closer to this instead: https://github.com/DiffSharp/DiffSharp/blob/dev/src/DiffSharp.Core/DiffSharp.Core.fsproj https://github.com/DiffSharp/DiffSharp/blob/dev/src/DiffShar... Once you have enough files, it might be a good idea to factor out separate concerns into different projects.
- fire_lake 2y agoYou can organize the files however you like, but you must specify list them in the correct order. The XML is not part of the language. You could invoke FSC manually (again, with the files listed in the correct order). It scales very well IME. Would C# be better with circular library dependencies?
- ReleaseCandidat 2y agoNo, it's not just about lexically scoping, but actually about having to have every (yes, some escape hatches exist like "namespace rec" and "module rec") definition before it's usage, which is an implementation detail of the compiler. So you have to order your files correctly when compiling. Which did break sometimes, because MS' build system/project files doesn't care about order, as C# doesn't care.
- p4bl0 2y agoI see. Thanks for correcting me (I'm quite familiar with OCaml but never used F# so this subtle difference between the two eluded me). I wouldn't have imagined this being put as an advantage in terms of robustness and reliability, contrary to lexical scoping and functions actually being closures.