While not possible today, there is actually nothing preventing you to
create a safe alternative (or even improving format so it works in this
way).
In a sense, a formatting function has two set of inputs:
- the format itself, from which you extract a set of constraints (expected
type-signature)
- the arguments to format, which can be seen as a single tuple (provided
type-signature)
And as long as you can ensure at compile time that you never attempt to
apply an "expected type-signature" to an incompatible "provided
type-signature", then you are safe.
I would suppose that as far as having runtime formats go, you would need to
introduce an intermediary step: the expected type-signature.
You could have a "Format" object, generic over the expected type-signature,
and a "new" constructor method taking a &str and returning an
"Option<Format<...>>".
Now, you have two phases:
- the "new" constructor checks, at runtime, that the specified format
matches the expected type-signature
- the compiler checks, at compile-time, that the provided type-signature
(arguments) match the expected type-signature (or it can be coerced to)
It might require variadic generics and subtle massaging of the type system,
however I do think it would be possible.
It might not be the best way to attack the issue though.
Post by Kevin BallardIt?s technically possible, but horribly unsafe. The only thing that makes
it safe to do normally is the syntax extension that implements `format!()`
ensures all the types match. If you really think you need this, you can
look at the implementation of core::fmt. But it?s certainly not appropriate
for localization, or template engines.
-Kevin Ballard
Post by Vadim ChugunovHi,
Is there any way to make Rust's fmt module to consume format template
specified at runtime?
Post by Vadim ChugunovThis might be useful for localization of format!'ed strings, or, if one
wants to use format! as a rudimentary template engine.
Post by Vadim Chugunov_______________________________________________
Rust-dev mailing list
Rust-dev at mozilla.org
https://mail.mozilla.org/listinfo/rust-dev
_______________________________________________
Rust-dev mailing list
Rust-dev at mozilla.org
https://mail.mozilla.org/listinfo/rust-dev
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/rust-dev/attachments/20140825/435863e6/attachment.html>