What do you thing about this string interpolation idea
Jonathan Marler
johnnymarler at gmail.com
Mon Dec 10 17:43:07 UTC 2018
On Monday, 10 December 2018 at 16:45:30 UTC, Steven Schveighoffer
wrote:
> On 12/10/18 11:36 AM, Aliak wrote:
>> On Monday, 10 December 2018 at 16:27:03 UTC, Steven
>> Schveighoffer wrote:
>>> On 12/10/18 11:11 AM, aliak wrote:
>>>> This is much better than having to mixin everywhere. A
>>>> couple of things:
>>>>
>>>> 1) Can this be put in a module so that you don't have to
>>>> mixin(enableInterpolation) but instead "import interp =
>>>> std.interpolation;" or something similar?
>>>
>>> No, you need a local mixin. Doing that import just imports
>>> the *symbol* into your namespace, but it doesn't give access
>>> to your namespace to the symbol.
>>>
>>
>> Au :(. Yeah that makes sense. Then I’m not sure I see how this
>> improves things if it has to be mixed in to every scope you
>> want to use interpolation for. The sparseness of interpolation
>> might just make mixin(Interp!””)); more appealing.
>
> The benefit is that you only have to mixin once, whereas the
> usage does not require a mixin. It just goes next to your
> import statements.
>
> However, multiple scopes may make this less appealing, as you
> would have to mixin at any inner scope that has a variable you
> want to deal with.
>
> But I plan to write some string interpolation libraries based
> on this, would love to see a "better SQL" library for something
> like this.
>
> Not sure if it mitigates the need for an interpolation DIP, as
> clearly this is going to be compile-time intensive, where the
> cleverness is stomped on by memory usage and slow compile times.
>
> -Steve
It can also have some pretty unexpected results. For example,
having a variable reference an outerscope instance instead of the
local one:
import std.stdio;
template somefun()
{
auto iterpolate(string s)()
{
//do some parsing
return mixin(s[1 .. $]);
}
}
enum enableInterpolate = "mixin somefun A; alias interpolate =
A.iterpolate;";
mixin(enableInterpolate);
enum a = 10;
enum msg = iterpolate!("$a");
void main()
{
int a = 5;
iterpolate!("$a").writeln;
}
This prints 10.
More information about the Digitalmars-d
mailing list