Created
August 26, 2016 21:01
-
-
Save bennn/81c3c0e66bf8c36d94eeb8e7a3999cbd to your computer and use it in GitHub Desktop.
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
#lang racket | |
(require | |
(prefix-in - (only-in racket/base struct)) | |
(for-syntax syntax/parse racket/syntax)) | |
(define-syntax (struct stx) | |
(syntax-parse stx | |
[(_ name (field* ...) other-arg* ...) | |
#:with define-name (format-id stx "define-~a" (syntax-e #'name)) | |
#:with (name-field ...) (for/list ([field (in-list (syntax-e #'(field* ...)))]) | |
(format-id stx "~a-~a" (syntax-e #'name) (syntax-e field))) | |
#'(begin (-struct name (field* ...) other-arg* ...) | |
(define-syntax (define-name stx) | |
(syntax-parse stx | |
[(_ nm val* (... ...)) | |
#:with (nm.field* (... ...)) (for/list ([field (in-list (syntax-e #'(field* ...)))]) | |
(format-id stx "~a.~a" (syntax-e #'nm) (syntax-e field))) | |
#'(begin | |
(define nm (name val* (... ...))) | |
(define nm.field* val*) | |
(... ...))])))])) | |
(struct dog (name breed age)) | |
(define-dog my-dog "lassie" "collie" 5) | |
my-dog.name |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment