2 ms·
> Haskell (I think) Not Haskell, record fields are accessed via automatically generated accessor functions e.g. after defining data Car = Car {company ::
by masklinn 11y ago
> Haskell (I think)
Not Haskell, record fields are accessed via automatically generated accessor functions e.g. after defining
data Car = Car {company :: String, model :: String, year :: Int}
you'd have
company :: Car -> String
model :: Car -> String
year :: Car -> Int
> And why is it uncommon to have something like get(obj, key)? Or like in Clojure, `(get {:key :val} :key)`? Seems more consistent, and uses less syntax.
Isn't get for collections? While historical javascript conflates records and hashmaps, most languages have a rather strict difference between the two, including clojure I believe. And do provide some sort of `get` for maps (as a free function or as a method depending on the language's base paradigm). I know of few languages where accessing record attributes and map values uses the same syntax[0]
And in clojure get isn't even necessary as most collections also act as functions e.g. `(get {:key :val} :key)` can be replaced by `({:key :val} :key)`, or (for maps only, IIRC integers are not IFn) `(:key {:key :val})`
[0] natively anyway, I have seen e.g. Python libraries where maps were extended to provide attribute-access to their values.