3 ms·
I like the 'weakly pure' concept in D. A function like pure int frignate(database db, const config cfg); cannot change anything except the database object (
by qznc 10y ago
I like the 'weakly pure' concept in D. A function like
pure int frignate(database db, const config cfg);
cannot change anything except the database object (and anything reachable from it). Can not mutate the environment. Can not mutate the config object parameter. This is finer control than pure functional programming and safer than imperative/object-oriented programming.
- thinkpad20 10y agoI'm not sure how D's purity system works, but that doesn't seem very pure to me. Mutating the database object is a globally visible effect, after all.
- buzzybee 10y agoThe guarantee it's making is that it's not going to manipulate program state that is outside the scope of "db". That is a pretty big deal for a systems developer since, as I discovered while working with D, many standard library functions that you wouldn't have given a second thought about happen to do unpure things like set a processor flag. We aren't even talking about your own program. From an abstracted viewpoint, it's not great, since a whole database covers potentially a lot of scope, and you may not want to care about the details of your floating point calculations in hardware, but in a concrete sense this is totally correct!
- mej10 10y agoYou can achieve this level of control using monad transformers or extensible effects -- this is a standard technique in Haskell. frignate :: (MonadDB m) => Config -> m Int frignate cfg = do db <- getDB ... return 1 And it is composable, so if a function calls a function that uses one of the managed resources then the requirement propagates upward. And you can swap in non-IO based instances for testing, or whatever else you want.