4 ms·
I've been thinking about this recently. I like passing a struct if all/most of its attributes are used. But if the struct has, say, six attributes, and the func
by uryga 8y ago
I've been thinking about this recently. I like passing a struct if all/most of its attributes are used. But if the struct has, say, six attributes, and the function uses only two, it might be clearer if the function only took those two as parameters... While it might not be the most ergonomic solution, I think there's something to be said for clearly marking which parts of the struct are actually used in a function.
- AlexCoventry 8y agoIn languages with destructuring syntax, you get the best of both worlds.
- jstimpfle 8y agoHow so? It's just a syntax. It doesn't remove the physical coupling of elements that are infrequently used as a unit.
- uryga 8y agoYou're right, but it can still make it obvious to the reader that you don't actually use all of the struct in a function. I think GP meant something like this: (Haskell syntax) -- struct with three fields data Bar = Bar { x :: Int , y :: Int , z :: String } -- function from Bar to Baz foo :: Bar -> Baz foo (Bar _ _ z) = ... `foo` takes a whole `Bar`, but in the pattern matching bit you explicitly say you don't need x and y by using `_`. Also, by enabling an extension[0], you can instead do: foo :: Bar -> Baz foo (Bar {z}) = ... Which I guess gets pretty close to what I want. [0] NamedFieldPuns
- AlexCoventry 8y agoYeah, that's what I meant, especially the second example, which is closer to my experience from clojure.
- uryga 8y agoAlso, thinking about it further, it's relatively easy to "lift" a function `String → Baz` to operate on structs like `{x: Int, y: Int, z: String}` (see Haskell's Lens), but it's not really possible to do it the other way.