introspection | meta data

Hi I recently had to implement an algorthm in C, and found the time to give it a go in Haskell to aid in learning haskell. I found myself "needing" runtime meta information on data types (well tuples which are data (a,b) = (a,b)). Does haskell allow this ? Basically I need to loop over the fields in my record. I came up with functions like this: -- Toggle fields to/from generic value toggle sf origsf 0 = sf toggle sf origsf n | newValue == "*ALL" = newSF | otherwise = toggle newSF origsf (n-1) -- <--------- Loop through fields by No where newValue = toggleField (getSFField n sf) (getSFField n origsf) -- <---- accessor functions newSF = setSFField n sf newValue .... getSFField :: Int -> SearchFilter -> String getSFField 1 (x,_,_,_,_) = x getSFField 2 (_,x,_,_,_) = x .... setSFField :: Int -> SearchFilter -> String -> SearchFilter setSFField 1 (a,b,c,d,e) f = (f,b,c,d,e) setSFField 2 (a,b,c,d,e) f = (a,f,c,d,e) .... The only problem with this is that if I want to add or remove a field, I need to change a lot of code. In most OO lanaguages I could use "reflection" to loop through my fields. In C (which also doesnt have meta data) I got round it by using pointers in an array. To add or remove a field just requires adding or removing a pointer to my array which is one line of code. The only thought I had was of using lists, but the this would mean I loose pattern matching against all values at once which is appealing for calrify of code. So what is the general haskell approach to this type of introspection/meta data problem... ? Thanks, _________________________________________________________________ Protect your PC - get McAfee.com VirusScan Online http://clinic.mcafee.com/clinic/ibuy/campaign.asp?cid=3963

Basically I need to loop over the fields in my record. I came up with functions like this: ... setSFField :: Int -> SearchFilter -> String -> SearchFilter setSFField 1 (a,b,c,d,e) f = (f,b,c,d,e) setSFField 2 (a,b,c,d,e) f = (a,f,c,d,e) ....
Can you name these fields? If so, haskell has (sorta clumsy) named records, and you can select and update fields by name, and you can replace 'setSFField 3 sf x' with 'sf {somefield=x}'
The only thought I had was of using lists, but the this would mean I loose pattern matching against all values at once which is appealing for calrify of code.
You can pattern match lists, just replace () with [] above. You just can't get a type error if it's the wrong length (but see the previous thread). If your data is of variable length and one type, then lists are what you want. If it has names and you often only want one field, then records. If it's just bundling a small number of variously-typed values, and you typically need all of them at once, then tuples. Otherwise, there are usual data structures in the stdlib, like tables (haskell calls them finite maps). You could say FM is to record as list is to tuple, sorta ;)
So what is the general haskell approach to this type of introspection/meta data problem... ?
Others have given good answers for this, but I suspect you may have chosen the wrong data structure... A C array of pointers maps closest to a MutableArray, which is mostly a list with different performance. Unless you're casting pointers, in which case Dynamic types or the Generic stuff is maybe what you want. Or a redesign ;)
participants (2)
-
Crypt Master
-
Evan LaForge