I have written a couple of small, experimental virtual machines in Haskell, and I always use the State monad with the virtual machine data type as the state.<br> data VM a = VM { getAlpha :: Int , getBeta :: String , getGamma :: a }<br>
which is all well and good, but I inevitably end up writing code like this along with it:<br> putAlpha a (VM _ b c) = (VM a b c)<br> putBeta b (VM a _ c) = (VM a b c)<br> putGamma c (VM a b _) = (VM a b c)<br>Its useful because you can just create one monadic function that updates the state and pass one of the "put" functions as a parameter.<br>
updateVM :: (x -> VM a -> VM b) -> x -> State (VM b) ()<br> updateVM putFunc value = do { state <- get ; put (putFunc value state) }<br><br>...some algorithm...<br> do updateVM putAlpha 12<br> updateVM putBeta "Hello"<br>
return somthing<br><br>But writing the "put" functions become tedious for virtual machines with more fields in their type, especially if you need to add a field to the data type in the future. Could there be syntactic sugar added to generate a list of functions that update the fields of a data type?<br>
data VM a = VM { getAlpha/putAlpha :: Int , getBeta/putBeta :: String , getGamma/putGamma :: a }<br>Where the slash operator is optional, but if included in the code will cause the compiler to generate functions of the given names that update those fields.<br>
<br>Pros: one more time-saving feature implemented in syntactic sugar. The optional nature of the slash operator would give users a choice of whether or not to use it.<br>Cons: increases complexity of the syntax<br><br>I couldn't find such a suggestion on the mailing list, but something tells me this idea is too simple to have not been suggested before. Sorry if this is a redundant feature request.<br>
<br>