About string and int spliced together.

mw mingwu at gmail.com
Sun Jun 21 05:59:42 UTC 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!



More information about the Digitalmars-d mailing list