1

Suppose we have a datatype like this:

data NodeProperties = 
    NodeProperties
    { 
        p1 :: PropertyInfo Bool,
        p2 :: PropertyInfo [String],
        p3 :: PropertyInfo [String],
        p4 :: PropertyInfo Bool,
        p5 :: PropertyInfo [String]
    } 
    deriving (Show, Data, Typeable)

and we have a modify function that has this kind of clauses:

| p1Prefix /= Nothing =
    let 
        value = readBoolean p1Prefix
        newProperties = 
            properties
            { 
                p1 = (p1 properties){value = Just value}
            }
    in 
        modifyPropertiesNode pragmas newProperties
| p2Prefix /= Nothing =
    let 
        value = readStringList p2Prefix
        newProperties = 
            properties
            { 
                p2 = (p2 properties){value = Just value}
            }
    in 
        modifyPropertiesNode pragmas newProperties

In my opinion, it would be better to have code above generalized to something like the code below.

| p1Prefix /= Nothing =
    updateProperty p1 readBoolean p1Prefix pragmas
| p2Prefix /= Nothing =
    updateProperty p2 readStringList p2Prefix pragmas

In order to do something like this I would need to use variables at the left hand side of a record assignment, and it seems that Haskell does not like the idea. Is there any alternative to achieve this?

0 Answers0