June 20, 2020
On Thursday, 18 June 2020 at 09:13:53 UTC, Dennis wrote:
> On Thursday, 18 June 2020 at 09:00:42 UTC, zoujiaqing wrote:
>> Type should be automatically converted to string!
>

> You might be interested in Adam's string interpolation DIP though:
> https://github.com/dlang/DIPs/pull/186

So has D gotten so complicated under the hood that its not possible to d:

string sentence = i"I and ${expression}";

Cut I'm seeing some weird syntax in there. Nothing like anything from JavaScript as referenced.
June 20, 2020
On Saturday, 20 June 2020 at 11:09:12 UTC, aberba wrote:
> So has D gotten so complicated under the hood that its not possible to d:

In that proposal, i"" is a string BUILDER, not a string. So it returns an object you can get a string out of, or do other things with too.

C# and Javascript also work this way, you just don't notice it as much due to their different typing systems: the C# object implicitly converts to string on demand, inserting a hidden call to its toString method when you assign it like that, and the Javascript one is passed to a function that does the conversion with special built-in syntax. JS' foo`$bar` actually calls the function foo to convert the string builder object to whatever you want, and if you don't specify a function, the language inserts a hidden toString function for you automatically.

The DIP there works the same way as those, just without the hidden function calls. You need to choose what function you want to call yourself.

// so this works since you call the idup method yourself
// telling it to copy the result of the builder into a
// plain string
string sentence = i"I and ${expression}".idup;

Or you can call other functions and avoid copying for maximum efficiency and flexibility - no hidden functions to surprise the low-level D crowd while only needing a very simple function call if you don't care about that and just want to keep it simple.

Similar to how D's map, filter, etc. may need the extra call to .array in some use cases. It looks like extra work at first glace, but it actually enables better code once you get to know it.
June 20, 2020
On Saturday, 20 June 2020 at 13:57:38 UTC, Adam D. Ruppe wrote:
> On Saturday, 20 June 2020 at 11:09:12 UTC, aberba wrote:
>> [...]
>
> In that proposal, i"" is a string BUILDER, not a string. So it returns an object you can get a string out of, or do other things with too.
>
> [...]

I glad it works that way. It was clear what was happening there.
June 21, 2020
On Thursday, 18 June 2020 at 09:00:42 UTC, zoujiaqing wrote:
> Now dlang string processing is complex, like this:

We've talked too much on this topic, I've found a number of DIPs, some string interpolation dub packages, either no-longer maintained, or does not fit my need. So I decided to just-do-it:

https://code.dlang.org/packages/jdiutil


jdiutil: Just-Do-It util mixin

Some small util mixin to make (debug) life easier:

-- string interpolation for easy debug print: _S with var name; _s without var name
-- ToString will generate string with class fields content, instead of just plain pointer.
-- ReadOnly, ReadWrite declare fields without boilerplate code
-- Singleton, Low-Lock Singleton Pattern https://wiki.dlang.org/Low-Lock_Singleton_Pattern
-- AtomicCounted, atomic counter


Examples, check app.d:

--------------------------------
public import jdiutil;

class Point {
  // declare fields
  mixin ReadOnly! (int,     "x");
  mixin ReadWrite!(double,  "y");
  mixin ReadWrite!(string,  "label", "default value");

  // atomic counter
  mixin AtomicCounted;

  // this is a Singleton class!
  mixin Singleton!Point;

  // debug print string helper
  mixin ToString!Point;
}

void main() {
        int i = 100;
        double d = 1.23456789;
        Point thePoint = Point.getSingleton();

        // multiple vars separated by ';'
        // _S with var name; _s without var name
        writeln(mixin(_S!"print with    var name: {i; d; thePoint}"));
        writeln(mixin(_s!"print without var name: {i; d; thePoint}"));

        thePoint.incCount();
        logger.info(mixin(_S!"works in logger too: {i; d; thePoint}"));

        thePoint.incCount();
        string str = mixin(_S!"assign to string with custom format: {i; d%06.2f; thePoint}");
        writeln(str);
}
--------------------------------


Output:
--------------------------------
print with    var name: i=100 d=1.23457 thePoint=app.Point(_x=0 _y=nan _label=default value _counter=0)
print without var name: 100 1.23457 app.Point(_x=0 _y=nan _label=default value _counter=0)
2020-06-20T22:31:29.053 [info] app.d:38:main works in logger too: i=100 d=1.23457 thePoint=app.Point(_x=0 _y=nan _label=default value _counter=1)
assign to string with custom format: i=100 d=001.23 thePoint=app.Point(_x=0 _y=nan _label=default value _counter=2)
--------------------------------


This is my 1st dub package, suggestions welcome (please use the github issue, easier to track.)

Thanks for everyone on this forum who have helped!

July 30, 2020
On Thursday, 18 June 2020 at 09:13:53 UTC, Dennis wrote:
> On Thursday, 18 June 2020 at 09:00:42 UTC, zoujiaqing wrote:
>> Type should be automatically converted to string!
>
> While this could be made to work for floating point numbers, it is ambiguous for integers since integers are often implicitly convertible to characters:
>
> writeln("AB"~67); // prints "ABC", code unit 67 represents 'C'
>
> You might be interested in Adam's string interpolation DIP though:
> https://github.com/dlang/DIPs/pull/186


It's still too complicated.

The simplest way:

int i = 99;

string a = "text " ~ i ~ "!";

writeln(a); // text 99!
July 30, 2020
On Thu, Jul 30, 2020 at 06:59:11PM +0000, zoujiaqing via Digitalmars-d wrote: [...]
> The simplest way:
> 
> int i = 99;
> 
> string a = "text " ~ i ~ "!";
> 
> writeln(a); // text 99!

You could just use std.conv.text:

	int i = 99;
	string a = text("blah ", i, "!");
	writeln(a);	// blah 99!


T

-- 
MASM = Mana Ada Sistem, Man!
July 31, 2020
On 7/30/20 2:59 PM, zoujiaqing wrote:
> On Thursday, 18 June 2020 at 09:13:53 UTC, Dennis wrote:
>> On Thursday, 18 June 2020 at 09:00:42 UTC, zoujiaqing wrote:
>>> Type should be automatically converted to string!
>>
>> While this could be made to work for floating point numbers, it is ambiguous for integers since integers are often implicitly convertible to characters:
>>
>> writeln("AB"~67); // prints "ABC", code unit 67 represents 'C'
>>
>> You might be interested in Adam's string interpolation DIP though:
>> https://github.com/dlang/DIPs/pull/186
> 
> 
> It's still too complicated.
> 
> The simplest way:
> 
> int i = 99;
> 
> string a = "text " ~ i ~ "!";
> 
> writeln(a); // text 99!

what's complicated about:

string a = i"text $i!".idup;

-Steve
July 31, 2020
On Friday, 31 July 2020 at 13:56:00 UTC, Steven Schveighoffer wrote:
> On 7/30/20 2:59 PM, zoujiaqing wrote:
>> On Thursday, 18 June 2020 at 09:13:53 UTC, Dennis wrote:
>>> On Thursday, 18 June 2020 at 09:00:42 UTC, zoujiaqing wrote:
>>>> Type should be automatically converted to string!
>>>
>>> While this could be made to work for floating point numbers, it is ambiguous for integers since integers are often implicitly convertible to characters:
>>>
>>> writeln("AB"~67); // prints "ABC", code unit 67 represents 'C'
>>>
>>> You might be interested in Adam's string interpolation DIP though:
>>> https://github.com/dlang/DIPs/pull/186
>> 
>> 
>> It's still too complicated.
>> 
>> The simplest way:
>> 
>> int i = 99;
>> 
>> string a = "text " ~ i ~ "!";
>> 
>> writeln(a); // text 99!
>
> what's complicated about:
>
> string a = i"text $i!".idup;
>
> -Steve

Thanks Steve.

but about:

string a = i"text $i!".idup;

or:

string a = "text" ~ i ~ "!";

or:

string a = "text {$i}!";


I think the latter two are simpler ;)



Ref: https://www.php.net/manual/en/language.types.string.php
August 03, 2020
On 7/31/20 2:18 PM, zoujiaqing wrote:
> On Friday, 31 July 2020 at 13:56:00 UTC, Steven Schveighoffer wrote:
>> what's complicated about:
>>
>> string a = i"text $i!".idup;
>>
> 
> Thanks Steve.
> 
> but about:
> 
> string a = i"text $i!".idup;
> 
> or:
> 
> string a = "text" ~ i ~ "!";
> 
> or:
> 
> string a = "text {$i}!";
> 
> 
> I think the latter two are simpler ;)

So we have 2 alternatives here. Both are viable, and both have precedent. And without getting into the entire discussion again, I can tell you why I would not want them.

1. string a = "text" ~ i ~ "!";

For this to work, the compiler has to call a function to process i into string form. This could technically be done for integers without allocations. But in the general case it cannot:

string a = "text" ~ myStruct ~ "!";

Necessarily this has to be a conversion from the struct into a string, which then could be included in the concatenation. I'm not a fan of hidden allocations. I'd much rather see:

string a = "text" ~ myStruct.to!string ~ "!";

2. string a = "text {$i}!";

I'm sure you are aware that we will never see a feature just happen in existing strings like this. We absolutely need a marker for interpolated strings to distinguish interpolated strings from normal strings to prevent existing code breakage.

So I'm going to refashion this as another oft-requested idea, borrowing the DIP syntax a bit:

string a = i"text $i!";

The i prefix meaning "interpolated string".

Such a request is also reasonable. However, it's too meh. D has so much potential power with lowering and compile-time processing, that it would be a waste to force string interpolation this way. Yes, an important use case is creating a string on the heap with interpolated items, and assigning to a string. But for the extra suffix ".idup", you do get that possibility.

AND if your goal is NOT just to create a string, there are far more efficient and useful things that can be had by the proposal:

writefln(i"text $i"); // just works
auto row = db.selectRow(i"SELECT * FROM tbl1 WHERE col1 = $i and col2 = $j"); // i and j passed as parameters to avoid sql injection

I WOULD be OK with an interpolated string automagically casting to a string, which converts into a library call for allocation. But this would really have to be something outside the normal type system. For avoiding the explicit ".idup" call, I think this is probably not worth it. And as Adam says in his DIP, the compiler can suggest using .idup when you should.

> Ref: https://www.php.net/manual/en/language.types.string.php

I use string interpolation in php all the time, mostly for SQL query writing. Super-prone to SQL injection, but I'm not going to change it now...

-Steve
1 2 3
Next ›   Last »