Thread overview | ||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
February 04, 2016 Just because it's a slow Thursday on this forum | ||||
---|---|---|---|---|
| ||||
https://github.com/D-Programming-Language/phobos/pull/3971 -- Andrei |
February 05, 2016 Re: Just because it's a slow Thursday on this forum | ||||
---|---|---|---|---|
| ||||
Posted in reply to Andrei Alexandrescu | On Thursday, 4 February 2016 at 15:33:41 UTC, Andrei Alexandrescu wrote: > https://github.com/D-Programming-Language/phobos/pull/3971 -- Andrei People one github were asking for a dump function so they could do int a = 5; dump!("a"); // prints "a = 5" Here's a working version if anyone wants it but you have to use it like mixin dump!("a"); //------------------------------------------------ mixin template dump(Names ... ) { auto _unused_dump = { import std.stdio : writeln, write; foreach(i,name; Names) { write(name, " = ", mixin(name), (i<Names.length-1)?", ": "\n"); } return false; }(); } unittest{ int x = 5; int y = 3; int z = 15; mixin dump!("x", "y"); // x = 5, y = 3 mixin dump!("z"); // z = 15 mixin dump!("x+y"); // x+y = 8 mixin dump!("x+y < z");// x+y < z = true } |
February 05, 2016 Re: Just because it's a slow Thursday on this forum | ||||
---|---|---|---|---|
| ||||
Posted in reply to Tofu Ninja | On Friday, 5 February 2016 at 02:46:01 UTC, Tofu Ninja wrote:
> ...
It's kinda neat cus it supports arbitrary expressions. Mixins are pretty powerful.
|
February 07, 2016 Re: Just because it's a slow Thursday on this forum | ||||
---|---|---|---|---|
| ||||
Posted in reply to Tofu Ninja | On 02/04/2016 09:46 PM, Tofu Ninja wrote:
> On Thursday, 4 February 2016 at 15:33:41 UTC, Andrei Alexandrescu wrote:
>> https://github.com/D-Programming-Language/phobos/pull/3971 -- Andrei
>
> People one github were asking for a dump function so they could do
> int a = 5;
> dump!("a"); // prints "a = 5"
>
>
> Here's a working version if anyone wants it but you have to use it like
> mixin dump!("a");
>
>
> //------------------------------------------------
>
> mixin template dump(Names ... )
> {
> auto _unused_dump = {
> import std.stdio : writeln, write;
> foreach(i,name; Names)
> {
> write(name, " = ", mixin(name), (i<Names.length-1)?", ":
> "\n");
> }
> return false;
> }();
> }
>
> unittest{
> int x = 5;
> int y = 3;
> int z = 15;
>
> mixin dump!("x", "y"); // x = 5, y = 3
> mixin dump!("z"); // z = 15
> mixin dump!("x+y"); // x+y = 8
> mixin dump!("x+y < z");// x+y < z = true
> }
This is useful and we should include it. How would you improve the code to allow dumping to a different File than stdout (e.g. most often stderr)? -- Andrei
|
February 08, 2016 Re: Just because it's a slow Thursday on this forum | ||||
---|---|---|---|---|
| ||||
Posted in reply to Andrei Alexandrescu | On Sunday, 7 February 2016 at 23:26:05 UTC, Andrei Alexandrescu wrote:
> On 02/04/2016 09:46 PM, Tofu Ninja wrote:
>> On Thursday, 4 February 2016 at 15:33:41 UTC, Andrei Alexandrescu wrote:
>>> https://github.com/D-Programming-Language/phobos/pull/3971 -- Andrei
>>
>> People one github were asking for a dump function so they could do
>> int a = 5;
>> dump!("a"); // prints "a = 5"
>>
>>
>> Here's a working version if anyone wants it but you have to use it like
>> mixin dump!("a");
>>
>>
>> //------------------------------------------------
>>
>> mixin template dump(Names ... )
>> {
>> auto _unused_dump = {
>> import std.stdio : writeln, write;
>> foreach(i,name; Names)
>> {
>> write(name, " = ", mixin(name), (i<Names.length-1)?", ":
>> "\n");
>> }
>> return false;
>> }();
>> }
>>
>> unittest{
>> int x = 5;
>> int y = 3;
>> int z = 15;
>>
>> mixin dump!("x", "y"); // x = 5, y = 3
>> mixin dump!("z"); // z = 15
>> mixin dump!("x+y"); // x+y = 8
>> mixin dump!("x+y < z");// x+y < z = true
>> }
>
> This is useful and we should include it. How would you improve the code to allow dumping to a different File than stdout (e.g. most often stderr)? -- Andrei
Something like this?
import std.stdio : File, stderr, stdout;
template dumpTo(alias output)
if (isInstanceOf!(File, output))
{
mixin template dumpTo(Names ... )
{
auto _unused_dump = {
import std.traits : Select;
foreach (i, name; Names)
{
output.write(name, " = ", mixin(name),
Select!(i < Names.length - 1, ", ", '\n'));
}
return false;
}();
}
}
alias dump = dumpTo!stdout;
alias errDump = dumpTo!stderr;
|
February 08, 2016 Re: Just because it's a slow Thursday on this forum | ||||
---|---|---|---|---|
| ||||
Posted in reply to John Colvin | On 2/7/16 7:11 PM, John Colvin wrote:
> alias dump = dumpTo!stdout;
> alias errDump = dumpTo!stderr;
I'm hoping for something with a simpler syntax, a la dump!(stdout, "x") where stdout is optional. -- Andrei
|
February 08, 2016 Re: Just because it's a slow Thursday on this forum | ||||
---|---|---|---|---|
| ||||
Posted in reply to Andrei Alexandrescu | On Monday, 8 February 2016 at 13:37:19 UTC, Andrei Alexandrescu wrote:
> On 2/7/16 7:11 PM, John Colvin wrote:
>> alias dump = dumpTo!stdout;
>> alias errDump = dumpTo!stderr;
>
> I'm hoping for something with a simpler syntax, a la dump!(stdout, "x") where stdout is optional. -- Andrei
dump and fdump, just like printf and fprintf?
|
February 08, 2016 Re: Just because it's a slow Thursday on this forum | ||||
---|---|---|---|---|
| ||||
Posted in reply to Andrei Alexandrescu | On Monday, 8 February 2016 at 13:37:19 UTC, Andrei Alexandrescu wrote: > On 2/7/16 7:11 PM, John Colvin wrote: >> alias dump = dumpTo!stdout; >> alias errDump = dumpTo!stderr; > > I'm hoping for something with a simpler syntax, a la dump!(stdout, "x") where stdout is optional. -- Andrei How about this, which allows you to specify variables as alias parameters (i.e. without strings) as well. It could be a lot neater if a static assert is used in the body instead of using template constraints, but obviously that has its downsides. import std.stdio : File; import std.traits : isSomeString; import std.meta : allSatisfy; private template isAlias(a ...) if (a.length == 1) { enum isAlias = __traits(compiles, { alias b = a[0]; }) && is(typeof(a[0])); } private template isStringValue(a ...) if (a.length == 1) { enum isStringValue = isSomeString!(typeof(a[0])); } private template isStringOrAlias(a ...) if (a.length == 1) { /* can't use templateOr in the dump template constraints * because `Error: template instance F!(a) cannot use local 'a' * as parameter to non-global template templateOr(T...)` */ * enum isStringOrAlias = isAlias!a || isStringValue!a; } mixin template dump(alias file, Args ...) if (is(typeof(file) == File) && Args.length > 0 && allSatisfy!(isStringOrAlias, Args)) { auto _unused_dump = { import std.traits : Select; // can put expressions directly in Select with // https://github.com/D-Programming-Language/phobos/pull/3978 enum sep = ", "; enum term = "\n"; foreach (i, arg; Args) { static if (isSomeString!(typeof(arg))) file.write(arg, " = ", mixin(arg), Select!(i < Args.length - 1, sep, term)); else file.write(__traits(identifier, Args[i]), " = ", arg, Select!(i < Args.length - 1, sep, term)); } return false; }(); } mixin template dump(Args ...) if (Args.length > 0 && allSatisfy!(isStringOrAlias, Args)) { import std.stdio : stdout; mixin .dump!(stdout, Args); } unittest { import std.stdio; int a = 3, b = 4; mixin dump!q{ a + b }; mixin dump!(stderr, "a - b"); mixin dump!a; mixin dump!(stderr, a, b); } |
February 08, 2016 Re: Just because it's a slow Thursday on this forum | ||||
---|---|---|---|---|
| ||||
Posted in reply to Andrei Alexandrescu | On Sunday, 7 February 2016 at 23:26:05 UTC, Andrei Alexandrescu wrote:
> On 02/04/2016 09:46 PM, Tofu Ninja wrote:
>> On Thursday, 4 February 2016 at 15:33:41 UTC, Andrei Alexandrescu wrote:
>>> https://github.com/D-Programming-Language/phobos/pull/3971 -- Andrei
>>
>> People one github were asking for a dump function so they could do
>> int a = 5;
>> dump!("a"); // prints "a = 5"
>>
>>
>> Here's a working version if anyone wants it but you have to use it like
>> mixin dump!("a");
>>
>>
>> //------------------------------------------------
>>
>> mixin template dump(Names ... )
>> {
>> auto _unused_dump = {
>> import std.stdio : writeln, write;
>> foreach(i,name; Names)
>> {
>> write(name, " = ", mixin(name), (i<Names.length-1)?", ":
>> "\n");
>> }
>> return false;
>> }();
>> }
>>
>> unittest{
>> int x = 5;
>> int y = 3;
>> int z = 15;
>>
>> mixin dump!("x", "y"); // x = 5, y = 3
>> mixin dump!("z"); // z = 15
>> mixin dump!("x+y"); // x+y = 8
>> mixin dump!("x+y < z");// x+y < z = true
>> }
>
> This is useful and we should include it. How would you improve the code to allow dumping to a different File than stdout (e.g. most often stderr)? -- Andrei
FWIW, That functionality's been in the "semitwist d tools" util lib for years. (Although granted, much of the lib has bitrotted by this point.) See "traceVal".
I'd wanted to do a non-mixin version, but that hasn't been possible. That was a major motivator for my suggestion ages ago of "string mixins without the mixin keyword by marking a ctfe func as mixin" but that was rejected in favor of only allowing it for template mixins, not string mixins.
|
February 09, 2016 Re: Just because it's a slow Thursday on this forum | ||||
---|---|---|---|---|
| ||||
Posted in reply to Andrei Alexandrescu | On Monday, 8 February 2016 at 13:37:19 UTC, Andrei Alexandrescu wrote:
> On 2/7/16 7:11 PM, John Colvin wrote:
>> alias dump = dumpTo!stdout;
>> alias errDump = dumpTo!stderr;
>
> I'm hoping for something with a simpler syntax, a la dump!(stdout, "x") where stdout is optional. -- Andrei
Why would `stdout` need to be a template argument?
|
Copyright © 1999-2021 by the D Language Foundation