5 ms·
I can't help thinking that this is just the wrong way to do this. If I have a subtle metaprogramming problem that I'm going to solve with the C preprocessor, I
by ajross 12y ago
I can't help thinking that this is just the wrong way to do this.
If I have a subtle metaprogramming problem that I'm going to solve with the C preprocessor, I can code it and document it right there in the same source where it's used. Because that's the natural place for it. The metacode is the fundamental thing I'm writing, and it belongs in the "code" files for the rest of the project.
These Rust things are implemented as modules. I need to keep them separate. I need to build them separately. That breaks discoverability really badly. If a future maintainer needs to figure out what my strange macros do, they just look at them. If they get confused by my strange syntax extensions, they need to figure out where some other crate is coming from.
Regardless of other concerns about the mechanics, I think this is going to put a really serious damper on this kind of code.
Quite honestly, I'm looking at this and seriously thinking that more pedestrian options (python scripts to emit or preprocess rust code, for example) might be simpler and cleaner in practice.
- mbrubeck 12y agoRust also has macros, which can easily be defined next to their use: http://doc.rust-lang.org/book/macros.html http://doc.rust-lang.org/book/macros.html Syntax extensions are more like plugins for the Rust compiler.
- ajross 12y agoI know, but Rust macros seem to be fairly simple "expansion of a syntax tree" things, not intended as a programming environment. This is (or at least appears to be) the Rust answer for things like elaborate C macros or C++ template metaprogramming. There's a place for that kind of code in the world, even though it's needed rarely. Thinking about this as a "compiler plugin" is exactly what I think I'm getting at: if I need to implement some sort of DSL feature for my code, I want to do it as part of my code and not have to deploy it as a plugin to the compiler.
- mbrubeck 12y agoWell, you can still have your syntax extension live in the same repo as the rest of your code. For example, the Servo repo has a "components/plugins" directory that contains our custom lints and other extensions. And other code in the repo just links to it like this: Cargo.toml: [dependencies.plugins] path = "../plugins" lib.rs: #![plugin(plugins)] This doesn't seem too different from the situation in C where you put your cpp macros in a header file so you can #include them in multiple source files.
- pcwalton 12y ago> This is (or at least appears to be) the Rust answer for things like elaborate C macros or C++ template metaprogramming. Generics are Rust's answer to most template metaprogramming. > if I need to implement some sort of DSL feature for my code, I want to do it as part of my code and not have to deploy it as a plugin to the compiler. The problem with that is that, in a cross-compilation scenario, the environment that the compiler runs in may be completely different from the environment that your code is deployed in. Different symbols may be available, some libraries may or may not be there or work differently, etc. So you really need it to be separate modules.
- ajross 12y agoThat seems kind of non-responsive. The fact that there are technical limitations with Rust's mechanism doesn't invalidate the fact that the existing solutions work fine when implemented with a cross compiler and yours don't. It's sort of a special case of my point: maybe you did it wrong. (Edit, for clarity: the C preprocessor and C++ template languages are well-specified interpreted languages executed at compile time. Rust's attempt here is poorly specified and coupled closely to the runtime environment of the compiler binary. And that choice has leaked into the API presented to the programmer. And that's bad.) And regarding templates/generics: I'm not talking about type calculus stuff, but more like the kind of code you see in Boost's lambda or serialization libraries (which to be fair I think are terrible libraries personally -- IMHO this kind of code doesn't belong in libraries at all, which is my point). I don't think Rust generics (to their credit) can do that.
- pcwalton 12y ago> That seems kind of non-responsive. The fact that there are technical limitations with Rust's mechanism doesn't invalidate the fact that the existing solutions work fine when implemented with a cross compiler and yours don't. It's sort of a special case of my point: maybe you did it wrong. Two of your examples have been template metaprogramming and cpp, both of which are very limited programming environments (technically Turing complete, but both are Turing tarpits). When you actually want a full programming environment, you always hit the cross-compilation issue. It's just fundamental to the problem. But the advantages of a having a full programming environment available, when you need that power, outweigh the downsides of having to write in a separate module. The other example you gave is writing a Python script, which has all the same problems of writing in a separate module, except now you're writing in a different language in addition to writing in a separate module. Sometimes you don't need the power of a real programming environment, of course. That's why the macro system, which does not expose a full programming environment, exists in Rust. But C++ templates and (especially!) cpp weren't designed as programming environments either. They're pretty terrible programming environments. If you want that kind of thing, use the macro system. It's technically Turing complete too in just the same way as templates are. But we also expose a full programming environment, if you want it. I'm mystified by how offering a feature that extends the existing state of the art in addition to offering a feature that reflects the state of the art—the macro system—could possibly be somehow "doing it wrong".
- dbaupp 12y agoRust macros are more powerful than CPP in many ways, e.g. pattern matching and nice variadics. Much easier to write, use and document than either a C macro or C++ templates (although the later is definitely improving in the newest standards). These sorts of macros work very well for large classes of things one would do in with those C/C++, and more beyond that too. Syntax extensions are particularly good at things that not possible without massive hacks in either (although, again, this is somewhat changing with constexpr in C++14 etc). On the point of being inline, yes, it would be helpful sometimes, but often syntax extensions are "standalone": useful functionality that makes sense to be in its own crate (e.g. for publishing to crates.io), especially while they are unstable. And cargo makes it so easy to use other crates (even local ones, even plugins) that it barely matters where it is defined in terms of tooling/infrastructure.
- progman 12y ago> I can't help thinking that this is just the wrong way to do this. I think this is a typical example for the huge verbosity of Rust. Yes, you get safe bare metal access but for the price of a lot of coding effort for _every_ application. For comparison watch how easily syntax extensions are done in Nim. For instance, template times(x: expr, y: stmt): stmt = for i in 1..x: y 10.times: echo "Hello World" http://hookrace.net/blog/what-is-special-about-nim/ http://hookrace.net/blog/what-is-special-about-nim/
- kibwen 12y agoUsing this as an example of Rust's verbosity is uninformed. The reason that syntax extensions are undocumented is because they are highly unstable and not intended for end-user consumption (users of Rust 1.0 will not be able to create syntax extensions). They operate by reaching deep into the guts of the compiler and directly playing with APIs that have been designed for consumption by compiler authors. That they are exposed at all in the unstable branch of Rust is because Rust is a language that prefers to shove as much logic out of the compiler and into the stdlib as it can, and syntax extensions are one mechanism by which this is currently achieved. Furthermore, your example is misguided. Syntax extensions are not for trivial rewriting, that's the purpose of macros. Syntax extensions are for doing things like expanding a regex into a finite state machine at compile-time, or providing custom extensions to Rust's type system. In terms of tools, they are a 40-foot industrial lathe which can do anything, but is overkill for the vast majority of tasks.
- progman 12y ago> Syntax extensions are not for trivial rewriting, that's the purpose of macros. Syntax extensions are for doing things like expanding a regex into a finite state machine at compile-time. Nim provides templates and macros for that. Templates are for rewriting while macros can create arbitrary AST nodes. http://blog.ldlework.com/2015/05/01/a-cursory-look-at-meta-programming-in-nim/ http://blog.ldlework.com/2015/05/01/a-cursory-look-at-meta-p...