Discussion:
[rust-dev] Can macro use variable values?
Daneel Yaitskov
2014-09-17 14:04:18 UTC
Permalink
Hi,


Does Rust macro have flow control based on a variable value?

I didn't find such info here http://doc.rust-lang.org/guide-macros.html.

I'd like to get a macro expading a for loop.

doTimes(3, println!("DDDD"))
=>
println!("DDDD")
println!("DDDD")
println!("DDDD")
--
Daneel S. Yaitskov
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/rust-dev/attachments/20140917/d335941f/attachment.html>
John Clements
2014-09-24 15:30:41 UTC
Permalink
Post by Daneel Yaitskov
Hi,
Does Rust macro have flow control based on a variable value?
I didn't find such info here http://doc.rust-lang.org/guide-macros.html.
I'd like to get a macro expading a for loop.
doTimes(3, println!("DDDD"))
=>
println!("DDDD")
println!("DDDD")
println!("DDDD?)
Perhaps I?m misunderstanding you, but in general that?s impossible. Specifically, variable varues aren?t known at runtime. So, for instance, if you wrote

doTimes(x, println!(?DDDD?))

? there?s no way (in general) to know the value of ?x? until the program is run.

Is there some reason you can?t just use a function call, here?

John Clements
Evan G
2014-09-24 15:59:21 UTC
Permalink
The thought is that you should be able to do it with static variables
(ones that are known at compile time, like his example)

This is known as Compile Time Function Evaluation, I know there's been
some work on it, and you can probably search the RFCs to figure out
the status currently.

On Wed, Sep 24, 2014 at 11:30 AM, John Clements
Post by John Clements
Post by Daneel Yaitskov
Hi,
Does Rust macro have flow control based on a variable value?
I didn't find such info here http://doc.rust-lang.org/guide-macros.html.
I'd like to get a macro expading a for loop.
doTimes(3, println!("DDDD"))
=>
println!("DDDD")
println!("DDDD")
println!("DDDD?)
doTimes(x, println!(?DDDD?))
? there?s no way (in general) to know the value of ?x? until the program is run.
Is there some reason you can?t just use a function call, here?
John Clements
_______________________________________________
Rust-dev mailing list
Rust-dev at mozilla.org
https://mail.mozilla.org/listinfo/rust-dev
Loading...