这是indexloc提供的服务,不要输入任何密码
Skip to content

Allow the types created for structs to be specified manually instead of always generating them from the struct name #134

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

Merged
merged 1 commit into from
Dec 30, 2015

Conversation

lexi-lambda
Copy link
Member

This change affects all the struct definition forms as well as #:struct clauses in require/typed. It's something I've been wanting to change for a while, but I've only now gotten around to doing it. The change is simple and can probably best be explained in code.

(struct [point : Point] ([x : Real] [y : Real]))

This creates a struct called point, but the type generated for it is Point. The change to require/typed is the same.

(require/typed
 net/url-structs
 [#:struct [path/param : Path/Param]
           ([path : (U String 'up 'same)]
            [param : (Listof String)])])

Capitalization is, unsurprisingly, the main motivator behind this change, which lets both naming conventions be satisfied without needing any type aliases.

@samth
Copy link
Member

samth commented May 18, 2015

I like this idea, but I think it should probably be a new keyword option to struct, rather than adding additional syntax around the struct name.

@lexi-lambda
Copy link
Member Author

Sure, that works. Just add #:type-id or #:type-name as a one of the struct options?

@samth
Copy link
Member

samth commented May 18, 2015

I like #:type-name.

@samth
Copy link
Member

samth commented Nov 10, 2015

@lexi-lambda ping

@lexi-lambda
Copy link
Member Author

@samth I updated this to use #:type-name for both cases. Let me know if you have any comments—I’ll squash the commits and do a little git housekeeping once everything’s reviewed.

;; FIXME: unsound, but relied on in core libraries
;; #:guard ought to be supportable with some work
;; #:property is harder
(~optional (~seq #:guard guard:expr))
(~seq #:property prop:expr prop-val:expr))
...)))
...)
#:attr untyped #`(#,@(if (attribute mutable?) #'(#:mutable) #'())
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@samth This bit right here is sort of icky. I’d appreciate any tips on how to do this without the brittle repetition.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The simplest way would be to use a parameterized splicing syntax class that has the keyword in a list as an attribute, if present, and an empty list otherwise. Also binding it to an attribute of ... depth 1 would let you write the results as something like:

mutable?.kw ...
transparent?.kw ...

and so on.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I’m not sure I understand. Could you show me a simple example?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just composing this in the text window, but something like this:

(define-splicing-syntax-class (opt-kw kw)
  [pattern (~seq k:keyword)
    #:when (eq? kw (syntax-e #'k))
    #:with (kw ...) #'(k)]
  [pattern (~seq)
    #:with (kw ...) #'()])

and then

(~var mutable (opt-kw '#:mutable))

@samth
Copy link
Member

samth commented Dec 30, 2015

Looks good with comments addressed.

@lexi-lambda lexi-lambda force-pushed the custom-struct-type-names branch from 876e2ca to 81be660 Compare December 30, 2015 20:54
This allows the types generated by the struct form, as well as #:struct
clauses of require/typed, to be specified explicitly using a #:type-name
option. This allows the name of a struct and the type it is assigned to
be different.

Closes racket#261
@lexi-lambda lexi-lambda force-pushed the custom-struct-type-names branch from 81be660 to a3ca5ae Compare December 30, 2015 21:12
@lexi-lambda lexi-lambda merged commit a3ca5ae into racket:master Dec 30, 2015
@lexi-lambda lexi-lambda deleted the custom-struct-type-names branch December 30, 2015 21:13
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 this pull request may close these issues.

2 participants