-
Notifications
You must be signed in to change notification settings - Fork 0
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
82 struct #93
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
…populates the missing fields with nil.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This pull-request contains a "struct" implementation, which currently allows structures to be defined - as a list of fields, and objects created assuming all fields are present. One a struct has been created magical methods allow type-checking and field access/updates.
This will close #82, once complete.
Sample usage, which works with this pull-request:
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?
Adding accessors was a little tricky, but we store the name of the faux method in a map with the appropriate field as the value. This allows accesses to be found and updates to be made.
The biggest issue here is that we're doing extra checks on the "hot path", so the whole eval method is getting deeply complex and slower. I think that needs to be spun out into a different issue.