Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Look at "structure" #82

Closed
skx opened this issue Nov 12, 2022 · 0 comments · Fixed by #93
Closed

Look at "structure" #82

skx opened this issue Nov 12, 2022 · 0 comments · Fixed by #93
Assignees

Comments

@skx
Copy link
Owner

skx commented Nov 12, 2022

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:

   (struct person name age)

Generates a bunch of things:

  • Generates a constructor
    • (set! person (fn* (name age))
  • Generates a type check
    • `(set! person? (fn* (x) ... is x an instance of person)"
  • Generates accessors for each field
    • (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.

@skx skx self-assigned this Nov 12, 2022
skx added a commit that referenced this issue Nov 12, 2022
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.
@skx skx mentioned this issue Nov 12, 2022
7 tasks
@skx skx closed this as completed in #93 Nov 13, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant