You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
More stuff wasn't immediately obvious, but I guess there are setters to match the getters. I guess the complication for implementation would be the creation of the methods, dynamically.
The text was updated successfully, but these errors were encountered:
This pull-request contains a proof of concept "struct" implementation,
which currently allows structures to be defined - as a list of fields,
and objects created assuming all fields are present.
This will close#82, once complete.
Sample usage:
;; define a structure
(struct person name address)
;; create an instance
(set! self (person "Steve Kemp" "My home address, Helsinki, Finland"))
(print "struct.type is %s" (type self))
(print "struct.address contains:%s" (get self "address"))
(print "struct.name contains:%s" (get self "name"))
Here you'll notice that the struct is actually a hash, and we're
merely setting the named fields as key/val pairs.
This is pretty smart, all we've really done is recorded the fields
a structure should contain, and hooked into our execution to
instantiate a new hash when we see a named structure - before falling
back to invoking a command instead.
The bit that's missing? We want to have "type?" support, and we
want to have accessors created.
type? support is trivial if we copy/paste primitive.Hash into
primitive.Struct and add a "type" member. We just return:
struct-%s, primitive.Type
However adding accessors get's tricky. I could have another map
but getting type validaiton is gonna be a pain. It also seems like
if I continue to abuse/reuse the Hash type we could use something
siumilar to allow:
hash.field
To either get/set values and that feels like a useful piece of
syntatic sugar. Will experiment before committing.
Hacker news had a link to Zuo recently:
That has support for "struct", which seems to do a bunch of things. Given the following code:
Generates a bunch of things:
(set! person (fn* (name age))
(set! person-name (fn* (strct) (return strct.name)
(set! person-age (fn* (strct) (return strct.age)
More stuff wasn't immediately obvious, but I guess there are setters to match the getters. I guess the complication for implementation would be the creation of the methods, dynamically.
The text was updated successfully, but these errors were encountered: