On 4/29/2013 2:00 PM, Graydon Hoare wrote:
> Yes. Formalizing and completing the definition of the constant
> expression subgrammar would be extremely valuable. It's one of the
> major high-risk areas remaining the the design space. -Graydon
VHDL is a very different language from Rust (it's a hardware design
language, not a normal programming language), but it has the nicest
constant system I've worked with.

The rule is basically that constant expressions can contain:

Literals (i.e. 5, 0x20)
Expressions that depend on constants  (i.e. 0x5 + 10 * const_k)
Any "pure" function whose arguments are constant.   (i.e. 5 + f_pure(5,
const_k) )

It's this last rule that is truly beautiful. You can use the same
functions in both constant initialization and run-time code, with only
the requirement that they are pure. Pure functions are ones whose output
depends only on their arguments (and constants). Allowing functions to
initialize constants avoids the whole annoyance in C/C++ where you have
to use macros to make things compile-time const. It also allows a
surprising amount of compile-time optimization.

I don't know how realistic this is for constants in a language like rust
- but it would be very elegant if it could work.

Erik

 
_______________________________________________
Rust-dev mailing list
[email protected]
https://mail.mozilla.org/listinfo/rust-dev

Reply via email to