Jump to page: 1 25  
Page
Thread overview
Better string mixins
Dec 28, 2018
Michelle Long
Dec 28, 2018
Nicholas Wilson
Dec 28, 2018
SashaGreat
Dec 28, 2018
rikki cattermole
Dec 28, 2018
SashaGreat
Dec 28, 2018
SashaGreat
Dec 28, 2018
JN
Dec 28, 2018
rikki cattermole
Dec 28, 2018
drug
Dec 28, 2018
Adam D. Ruppe
Dec 28, 2018
Andre Pany
Dec 28, 2018
Walter Bright
Dec 28, 2018
John Colvin
Dec 29, 2018
Jonathan Marler
Dec 29, 2018
Walter Bright
Dec 29, 2018
Jonathan Marler
Dec 29, 2018
Walter Bright
Dec 29, 2018
bachmeier
Dec 30, 2018
Rubn
Dec 30, 2018
Mike Parker
Dec 29, 2018
bauss
Dec 29, 2018
Walter Bright
Dec 29, 2018
Rubn
Dec 29, 2018
Ethan
Dec 28, 2018
Neia Neutuladh
Dec 28, 2018
Paul Backus
Dec 28, 2018
Neia Neutuladh
Dec 29, 2018
Walter Bright
Dec 29, 2018
rikki cattermole
Dec 29, 2018
Walter Bright
Dec 29, 2018
bauss
Dec 29, 2018
Walter Bright
Dec 28, 2018
Rubn
Dec 29, 2018
Neia Neutuladh
Dec 29, 2018
rikki cattermole
Dec 29, 2018
Walter Bright
Dec 29, 2018
Rubn
Jan 02, 2019
Dgame
Jan 02, 2019
Bastiaan Veelo
Jan 02, 2019
Jordan Wilson
Jan 02, 2019
Dgame
Jan 02, 2019
Dgame
December 28, 2018
The problem with mixing in code is that one has to manually build code strings like:

static foreach(k; X)
   mixin("int Var"~k~" = "~k~";);

which is error prone, ugly, time consuming, and harder to read.

Instead, I propose a simple extension:

static foreach(k; X)
   mixin("int Var@k = @k;");


Here the @ simple, or whatever symbol would be best, takes the variable and inserts it from the outer scope as a string, converting it to a string if necessary.

It can only be used on such CT variables so errors should not exist.

If necessary, maybe

static foreach(k; X)
   @mixin("int Var@k = @k;");

or

static foreach(k; X)
   mixin(@"int Var@k = @k;");

could be used.


This avoids having to do shit like

mixin("foo("~x~", "~y~", "~z~");");

so one has

mixin("foo(@x,@y,@z);");

which translates almost directly in to standard code(one can simply strip the @'s).







December 28, 2018
On Friday, 28 December 2018 at 05:55:30 UTC, Michelle Long wrote:
> The problem with mixing in code is that one has to manually build code strings like:
>
> [...]

mixin accepts multiple arguments, see the bottom of https://dlang.org/spec/statement.html#mixin-statement
December 28, 2018
On Friday, 28 December 2018 at 05:55:30 UTC, Michelle Long wrote:
> The problem with mixing in code is that one has to manually build code strings like:
>
> static foreach(k; X)
>    mixin("int Var"~k~" = "~k~";);
>
> which is error prone, ugly, time consuming, and harder to read.
>
> Instead, I propose a simple extension:
>
> static foreach(k; X)
>    mixin("int Var@k = @k;");
>
>
> Here the @ simple, or whatever symbol would be best, takes the variable and inserts it from the outer scope as a string, converting it to a string if necessary.
>
> It can only be used on such CT variables so errors should not exist.
>
> If necessary, maybe
>
> static foreach(k; X)
>    @mixin("int Var@k = @k;");
>
> or
>
> static foreach(k; X)
>    mixin(@"int Var@k = @k;");
>
> could be used.
>
>
> This avoids having to do shit like
>
> mixin("foo("~x~", "~y~", "~z~");");
>
> so one has
>
> mixin("foo(@x,@y,@z);");
>
> which translates almost directly in to standard code(one can simply strip the @'s).

As far as I understand you propose a string interpolation syntax for string mixins only.

There is currently a DIP for adding general string interpolation  to the language. Also there is a library solution available for string interpolation.

With string interpolation your use case will become quite readable and less error prone.

When the DIP is in a good shape there might be a chance it is added to the language. I just found a post of Walter where he express his open mind for this topic.

Kind regards
Andre


December 28, 2018
On 12/27/2018 9:55 PM, Michelle Long wrote:
> This avoids having to do shit like

Please don't use such words on the forums. We expect professional demeanor.
December 28, 2018
On Friday, 28 December 2018 at 09:12:45 UTC, Nicholas Wilson wrote:
> On Friday, 28 December 2018 at 05:55:30 UTC, Michelle Long wrote:
>> The problem with mixing in code is that one has to manually build code strings like:
>>
>> [...]
>
> mixin accepts multiple arguments, see the bottom of https://dlang.org/spec/statement.html#mixin-statement

It would be nice if it were like C#:

mixin("x = {0}", x);

More arguments:

mixin("x = {0}; y = {1}", x, y);

S.
December 29, 2018
On 29/12/2018 1:29 AM, SashaGreat wrote:
> 
> It would be nice if it were like C#:
> 
> mixin("x = {0}", x);
> 
> More arguments:
> 
> mixin("x = {0}; y = {1}", x, y);
> 
> S.

No need.

import std.stdio;
import std.format;

void main() {
    int x;
    mixin("x = %d;".format(8));
    writeln(x);
}

Its hard to argue for syntax sugar when there is reasonable library code. Going to need to make a clear case argument for why this isn't acceptable. Otherwise a DIP for it would be rejected.
December 28, 2018
On Friday, 28 December 2018 at 12:34:27 UTC, rikki cattermole wrote:
> No need.
>
> import std.stdio;
> import std.format;
>
> void main() {
>     int x;
>     mixin("x = %d;".format(8));
>     writeln(x);
> }

Yes, but for the second case it would be ugly...

mixin("x = {0}; y = {1}", x, y);

S.
December 28, 2018
On Friday, 28 December 2018 at 12:43:28 UTC, SashaGreat wrote:
> On Friday, 28 December 2018 at 12:34:27 UTC, rikki cattermole wrote:
>> No need.
>>
>> import std.stdio;
>> import std.format;
>>
>> void main() {
>>     int x;
>>     mixin("x = %d;".format(8));
>>     writeln(x);
>> }
>
> Yes, but for the second case it would be ugly...
>
> mixin("x = {0}; y = {1}", x, y);
>
> S.

By the way, thinking more about it, in C# (My main language), in could easily create a extension for this like:

public static Format(string this, object[] args){
    // Loop through all the {x} replacing with arg[x]
}

Then

"x = {0}".Format(1);

"x = {0}; y = {0}".Format(10, 20);

Is this possible with D?

S.






}
December 29, 2018
On 29/12/2018 1:43 AM, SashaGreat wrote:
> On Friday, 28 December 2018 at 12:34:27 UTC, rikki cattermole wrote:
>> No need.
>>
>> import std.stdio;
>> import std.format;
>>
>> void main() {
>>     int x;
>>     mixin("x = %d;".format(8));
>>     writeln(x);
>> }
> 
> Yes, but for the second case it would be ugly...
> 
> mixin("x = {0}; y = {1}", x, y);
> 
> S.

mixin("x = %d; y = %d;".format(8, 4));

Looks ok to me, given that the original code was ugly as well (it can be improved as a multi-line string).
December 28, 2018
On 28.12.2018 15:51, rikki cattermole wrote:
> On 29/12/2018 1:43 AM, SashaGreat wrote:
>> On Friday, 28 December 2018 at 12:34:27 UTC, rikki cattermole wrote:
>>> No need.
>>>
>>> import std.stdio;
>>> import std.format;
>>>
>>> void main() {
>>>     int x;
>>>     mixin("x = %d;".format(8));
>>>     writeln(x);
>>> }
>>
>> Yes, but for the second case it would be ugly...
>>
>> mixin("x = {0}; y = {1}", x, y);
>>
>> S.
> 
> mixin("x = %d; y = %d;".format(8, 4));
> 
> Looks ok to me, given that the original code was ugly as well (it can be improved as a multi-line string).

In practice your mixin string is more complex and format doesn't work so nicely. Sometimes you need to use one argument in several places in mixin string also. But for the case above it works nice sure.
« First   ‹ Prev
1 2 3 4 5