-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
macro shared state #3604
Comments
A long time ago I wasn't sure about this, having stuff change at compile time. However, Elixir does this exact same thing. Well, it's a bit different, they use attributes which are available at compile time, but one can override them and use their previous values to create new ones (I guess mostly because everything's immutable). And then they have _before_compile. In fact, in Crystal you can already have shared state. We are just missing the require "json"
class Foo
MAPPING = {} of Nil => Nil
macro mapping(**values)
{% for key, value in values %}
{% MAPPING[key] = value %}
{% end %}
end
macro close_mapping
JSON.mapping({{MAPPING}})
end
mapping x: Int32, y: Int32
end
class Foo
mapping z: Int32?
end
class Foo
close_mapping
end
p Foo.from_json(%<{"x": 1, "y": 2}>)
p Foo.from_json(%<{"x": 1, "y": 2, "z": 3}>) We just need to define the name for this macro. It's a new macro hook, that will be executed, I think, right after all the top level declarations and methods are processed, right before instance vars types are checked, etc. |
I think There was an issue that since the constant can't change the value it reference to, then for value types we need to boxed them. But even with that limitation I think it's good to add this. Also someone can hack around with run command and save state in temp files and read them before compiling 😨 ... someone will eventually do it ... :-P |
Actually, a name in the past tense would be optimal, so it's on par with included, inherited, extended. Ideas? |
Maybe Edit: actually |
would be nice if macro can have shared variables, and callback after program readed and macro expanded (
macro class_finished
ormacro program_finished
).this allow things like #2987 or nice dsl, api generators, based on user usage:
shard
user program
The text was updated successfully, but these errors were encountered: