Two standard libraries?

Reiner Pope some at address.com
Tue Jul 17 00:35:16 PDT 2007


Don Clugston wrote:
> Robert Fraser wrote:
>> Regan Heath Wrote:
>>
>>>>> For example:
>>>>>
>>>>> "{0} has {1} pieces in {2}"
>>>>>
>>>>> Could be:
>>>>>
>>>>> "{supplier} has {stock} pieces in {city}"
>>>>>
>>>>> and make the translator happy.
>>>> How would such identifiers be matched up with variadic arguments?
>>> hmm.. Could it match the variable names with the inserts?
>>>
>>> string supplier = "Bob";
>>> string city = "Someplace far far away";
>>> int stock = 5;
>>>
>>> format("{supplier} has {stock} pieces in {city}",
>>>    supplier, city, stock);
>>>
>>> Maybe with some more reflection features it could be possible.
>>>
>>> Regan
>>>
>>>
>>  
>> Well, not even scripting languages keep names of local variables in 
>> memory at runtime, so I assume you mean compile-time. That means that 
>> the localized properties would have to be known at compile time (via 
>> file imports, etc.), which, assuming you store every language in the 
>> executable, might lead to some code bloat and a not-so-nimble 
>> architecture. But if that's what you're going for, someone 
>> demonstrated a while back that this is already possible, via mixins, 
>> and will get a lot easier with macros. Search for PHP-style print or 
>> something like that (I'm too lazy & limited by BlackBerry bandwidth 
>> to  get you the exact link).
> 
> That was me. It's true, no new reflection features are required. The 
> difficulty is in deciding how it should work, rather than implementing it.
> Specifically, what do you do if some of the variables are calculations?
> Eg, if stock is (palettesize*numberofpalettes*15) ?
> You could do
> format("{supplier} has {stock = (palettesize*numberofpalettes*15)} 
> pieces in {city}");
> But I don't think that's what was intended -- you don't want the 
> translator seeing the calculations!
> 
> You could have a separate string converting names to indices, but it's 
> starting to look clumsy.

Alternatively, you could generate a name-to-varargs-index AA at 
compile-time, and use this in your runtime string formatter. Something like:

int stock = ... ;
string supplier = ... ;
City city = ... ;
string formatstring = getLocalizedFormatString(); // runtime
mixin(DoRuntimeFormat!(formatstring, stock, supplier, city));

...

template DoRuntimeFormat(alias formatString, Names...)
{
     const char[] DoRuntimeFormat = "format(" + formatString.stringof
        + ", " + ConvertToAA!(Names) + ");";
}


   -- Reiner



More information about the Digitalmars-d mailing list