Evaluation of IO actions in record assignment

Hi. I'm toying around with GTK2Hs and one of the things I'm doing is stuffing a bunch of widgets in a record. The problem is that the function that fetches a widget from the Glade file returns IO Widget while my structure contains Widget's. The best I've come up with is simply evaluating the actions by putting the result in temporaries and then building the record. Is there an easier way to accomplish this? signatureEntry <- xmlGetWidget xml castToEntry "signatureEntry" passwordEntry <- xmlGetWidget xml castToEntry "passwordEntry" repeatEntry <- xmlGetWidget xml castToEntry "repeatEntry" return UserPanel {userPanelSignatureEntry = signatureEntry, userPanelPasswordEntry = passwordEntry, userPanelRepeatEntry = repeatEntry} Thanks, Adde

Try liftM3 from Control.Monad let get = xmlGetWidget xml castToEntry liftM3 UserPanel (get "signatureEntry") (get "passwordEntry") (get "repeatEntry") Adde wrote:
Hi. I'm toying around with GTK2Hs and one of the things I'm doing is stuffing a bunch of widgets in a record. The problem is that the function that fetches a widget from the Glade file returns IO Widget while my structure contains Widget's. The best I've come up with is simply evaluating the actions by putting the result in temporaries and then building the record. Is there an easier way to accomplish this?
signatureEntry <- xmlGetWidget xml castToEntry "signatureEntry" passwordEntry <- xmlGetWidget xml castToEntry "passwordEntry" repeatEntry <- xmlGetWidget xml castToEntry "repeatEntry" return UserPanel {userPanelSignatureEntry = signatureEntry, userPanelPasswordEntry = passwordEntry, userPanelRepeatEntry = repeatEntry}
Thanks, Adde
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe

Adde wrote:
signatureEntry <- xmlGetWidget xml castToEntry "signatureEntry" passwordEntry <- xmlGetWidget xml castToEntry "passwordEntry" repeatEntry <- xmlGetWidget xml castToEntry "repeatEntry" return UserPanel {userPanelSignatureEntry = signatureEntry, userPanelPasswordEntry = passwordEntry, userPanelRepeatEntry = repeatEntry}
Use one of the general monadic combinators given in Control.Monad: liftM3 UserPanel (xmlGetWidget xml castToEntry "signatureEntry") (xmlGetWidget xml castToEntry "passwordEntry") (xmlGetWidget xml castToEntry "repeatEntry") or return UserPanel `ap` xmlGetWidget xml castToEntry "signatureEntry" `ap` xmlGetWidget xml castToEntry "passwordEntry" `ap` xmlGetWidget xml castToEntry "repeatEntry" Tillmann

Use one of the general monadic combinators given in Control.Monad:
liftM3 UserPanel (xmlGetWidget xml castToEntry "signatureEntry") (xmlGetWidget xml castToEntry "passwordEntry") (xmlGetWidget xml castToEntry "repeatEntry")
or
return UserPanel `ap` xmlGetWidget xml castToEntry "signatureEntry" `ap` xmlGetWidget xml castToEntry "passwordEntry" `ap` xmlGetWidget xml castToEntry "repeatEntry"
Tillmann
Thanks! The problem is i really like specifying which fields in the record are assigned what. It's pretty easy to mess things up and for example assign the password-entry to the signature-field using your examples. Is there nothing I can do to xmlGetWidget to evaluate the actions when assigning? return UserPanel { userPanelSignatureEntry = *magic* xmlGetWidget xml castToEntry "signatureEntry", userPanelPasswordEntry = *magic* xmlGetWidget xml castToEntry "passwordEntry", userPanelRepeatEntry = *magic* xmlGetWidget xml castToEntry "repeatEntry"}
participants (3)
-
Adde
-
haskell@list.mightyreason.com
-
Tillmann Rendel