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.



On Mon, Aug 25, 2014 at 1:33 AM, Kevin Ballard <[email protected]> wrote:

> It’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
>
> > On Aug 24, 2014, at 2:48 PM, Vadim Chugunov <[email protected]> wrote:
> >
> > Hi,
> > Is there any way to make Rust's fmt module to consume format template
> specified at runtime?
> > This might be useful for localization of format!'ed strings, or, if one
> wants to use format! as a rudimentary template engine.
> > _______________________________________________
> > Rust-dev mailing list
> > [email protected]
> > https://mail.mozilla.org/listinfo/rust-dev
>
>
> _______________________________________________
> Rust-dev mailing list
> [email protected]
> https://mail.mozilla.org/listinfo/rust-dev
>
>
_______________________________________________
Rust-dev mailing list
[email protected]
https://mail.mozilla.org/listinfo/rust-dev

Reply via email to