-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Add #parse_type
#11126
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
Add #parse_type
#11126
Changes from 10 commits
7592d93
dd06433
c759a52
3b33eac
89ca4db
054e8da
7baf891
610bcf4
20a23d1
2e3eb9d
9523a45
0b49882
672524a
31d4765
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -153,6 +153,35 @@ module Crystal::Macros | |
| def host_flag?(name) : BoolLiteral | ||
| end | ||
|
|
||
| # Parses `self` into a `Path` or `Generic` (also used for unions). | ||
| # The `Path#resolve` or `Generic#resolve` method could then be used | ||
| # to resolve the value into a `TypeNode`, if the *type_name* represents a type, | ||
| # otherwise the value of the constant. | ||
| # | ||
| # A compile time error is raised if the type/constant does not actually exist, | ||
| # or if a required generic argument was not provided. | ||
| # | ||
| # ``` | ||
| # class Foo; end | ||
| # | ||
| # struct Some::Namespace::Foo; end | ||
| # | ||
| # module Bar(T); end | ||
| # | ||
| # MY_CONST = 1234 | ||
| # | ||
| # {{ parse_type("Foo").resolve.class? }} # => true | ||
| # {{ parse_type("Some::Namespace::Foo").resolve.struct? }} # => true | ||
| # {{ parse_type("Foo|Some::Namespace::Foo").resolve.union_types.size }} # => 2 | ||
| # {{ parse_type("Bar(Int32)|Foo").resolve.union_types[0].type_vars.size }} # => 1 | ||
| # {{ parse_type("MY_CONST").resolve }} # => 1234 | ||
| # | ||
| # {{ parse_type("MissingType").resolve }} # => Error: undefined constant MissingType | ||
| # {{ parse_type("UNKNOWN_CONST").resolve }} # => Error: undefined constant UNKNOWN_CONST | ||
|
Blacksmoke16 marked this conversation as resolved.
|
||
| # ``` | ||
| def parse_type(type_name : StringLiteral) : Path | Generic | ||
|
Blacksmoke16 marked this conversation as resolved.
Outdated
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hmm, thoughts on just adding them to the union to be more clear what it could return?
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is just a minor thing, so we can merge this right away and always improve the docs later on.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I already had it done, plus addressed your other comment, so I just pushed it up. Should be good to go now. |
||
| end | ||
|
|
||
| # Prints AST nodes at compile-time. Useful for debugging macros. | ||
| def puts(*expressions) : Nop | ||
| end | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.