fsprojects / fsharp-cheatsheet

This cheatsheet aims to succinctly cover the most important aspects of F# 6.0.
http://fsprojects.github.io/fsharp-cheatsheet/
Other
327 stars 59 forks source link

Code Organization Section #23

Closed SpiralOSS closed 11 months ago

SpiralOSS commented 11 months ago

@bartelink When I first started with F#, I had a little trouble with the namespace and module syntax when it was at the top of the file.

I made a section on Code Organization, but wanted to get some feedback before bringing it all the way to a PR.

https://github.com/SpiralOSS/fsharp-cheatsheet/blob/feat/code_organization/docs/fsharp-cheatsheet.md#files

If you have a moment, could you take a look and tell me whether it looks like it would be helpful?

Thanks GregH

bartelink commented 11 months ago

It's definitely worth covering, but its damn tricky to elevate the core concepts while keeping it scannable given how much stuff can overlap in there. In particular rec is probably worth pulling off to the side, and maybe the overall thing is better addressed by having sections about

Not sure that table truly works, even if I can see it straightening out quite a few things in a learners map of the world.

I't tend to have File, Namespace, Type and Module as headings

While there's a commonality between namespaces and module in that you

... I think that's not the point ...

The high order bits for me are:

I'd try to tersely cover

And then illustrate - possibly showing equivalence between a file-module and doing the same thing as a file with a namespace and the same module nested within. That section should show examples with very minimal text (ideally just terse comments). i.e. "or just read the intro already" is implied

Maybe show

namespace A.B

[<AutoOpen>]
module private Helpers =
    let calc x = x * 2 // A.B.calc
    let frob x = x + 1 

module C =
    let computeC () = frob 5
    do printfn "D initialized"
    module D = // externally, this is `A.B.C.D`
        let x = calc 5

vs

module A.B.C

let private frob x = x + 1
let computeC () = frob 5 // A.B.C.computeC

let private calc x = x * 2 // A.B.C.calc 
module D = // externally, this is `A.B.C.D`
    do printfn "D initialized"
    let x = calc 5

vs

module rec A.B.C

let computeC () = frob 5
module D = // externally, this is `A.B.C.D`
    do printfn "D initialized"
    let x = calc 5

let private calc x = x * 2 // A.B.C.calc 
let private frob x = x + 1

(Best to do a draft PR in circumstances like this; e.g. for me to hang other small comments such as: no need to the 'for the OO people' qualifiers)

SpiralOSS commented 11 months ago

Thank you for the lengthy response! I can see your point (about it not being the point). Let me digest and distill.

bartelink commented 11 months ago

I haven't read Stylish F#, F# in Action or https://leanpub.com/essential-fsharp, nor have I tutored anyone new to F# for quite some time so don't necessarily have any particularly well thought out ways of presenting these things

I read Expert F# twice, Real world FP by Petricek/Skeet ages ago mainly because there was not that much more when I was learning. Expert F# spoke to me, but it's defintely not succinct.

(Also I trust you've read Domain Modelling made Functional - if not, it's definely a must read)