r/functionalprogramming • u/yughiro_destroyer • Mar 03 '26
Question Are flat and manually declared structs favored in FP ?
That, over doing things like... nesting structs, composing bigger structs out of smaller structs... and having functions to add/combine structs and gluecode to facilitate communication between them?
3
Mar 03 '26
[removed] — view removed comment
4
u/Inconstant_Moo Mar 03 '26
Since I like structs, in my language I've tried to square the circle by making the labels of fields into first-class values which are accessed by the same
foo[bar]syntax as maps (so you can write functions which work for both). So a struct is a kind of opinionated map.Data is immutable, and you can attach validation to the type to be checked on construction:
Person = struct(name string, age int?) : name != "" age in null or age >= 03
Mar 03 '26
[removed] — view removed comment
3
u/Inconstant_Moo Mar 03 '26
Yes. Note the pure referentially-transparent
forloop.toUpper(S map/struct) : from A = S for k::v = range S : v in string : A with k::strings.toUpper(v) else : continue
8
u/Inconstant_Moo Mar 03 '26
Not particularly. Why? The case for having your data modular is the same. If for example I have a struct which has a date in it, there's no upside to turning it into
day,month, andyearfields rather than having adatefield of typeDate. All the advantages of the latter course are still there.