Re: Feature or bug: print braces

2015-05-15 Thread Ivan Kazmenko via Digitalmars-d-learn

On Thursday, 14 May 2015 at 00:29:06 UTC, Dennis Ritchie wrote:

Why doesn't the compiler produces an error?

-
import std.stdio;

void main() {
writeln({});
}
-
http://ideone.com/qTZCAd


Somehow reminds me of this lambda:
https://github.com/Hackerpilot/Idiotmatic-D/blob/master/idiotmatic.d#L127-L128


Re: Feature or bug: print braces

2015-05-15 Thread Dennis Ritchie via Digitalmars-d-learn

On Friday, 15 May 2015 at 08:44:41 UTC, Ivan Kazmenko wrote:

Somehow reminds me of this lambda:
https://github.com/Hackerpilot/Idiotmatic-D/blob/master/idiotmatic.d#L127-L128


Maybe it generally blocks for initialization of variables:
https://github.com/Hackerpilot/Idiotmatic-D/blob/master/idiotmatic.d#L130-L131

-
for ( { int i = 0; } i  5; ++i ) {
writeln(test);
}

It seems to me that this should be fixed :)

-
int x;

void foo(int tmp) {
x = tmp;
}
...
writeln( { int i = 5; writeln(i); foo(i); });
writeln(x);


Re: Feature or bug: print braces

2015-05-14 Thread Ali Çehreli via Digitalmars-d-learn

On 05/14/2015 03:39 PM, Dennis Ritchie wrote:

On Thursday, 14 May 2015 at 21:55:40 UTC, Alex Parrill wrote:

On Thursday, 14 May 2015 at 00:39:25 UTC, Dennis Ritchie wrote:

On Thursday, 14 May 2015 at 00:33:33 UTC, Brian Schott wrote:

You told it to output a function literal, so it did.


Yes, but it would be logical to deduce something like:
-
writeln({}); // prints literal[{}]

Or the compiler will not be able to distinguish the literal from the
ordinary function arguments?


Literal what?

Associative array? That uses square brackets, not curly brackets.
Struct? What struct would you be creating? And curly braces only works
for initialization.
Lambda? Well you can omit the parameters if there are none, and `{}`
in a lambda will give you a block to write, so `{}` is a valid lambda
that accepts nothing and does nothing.


I just wanted to say that writeln function of demand should not print
anything else at this challenge, not 804CF88 :)

-
writeln({});


Yes, it is weird but that value happens to be the address of the 
function. Here is another test:


import std.stdio;

void foo() pure nothrow @nogc @safe
{}

void main()
{
void printInfo(T)(T t)
{
writefln(%s %s, T.stringof, t);
}

auto f = (){};// -- Why the need for () here?

printInfo(foo);
printInfo(f);
printInfo({});// -- No need for () here.
}

There is an inconsistency where a lambda need to be defined with empty 
parentheses in one context while it is just fine without in another context.


One output shows that they are all of the same type (function pointers):

void function() pure nothrow @nogc @safe 473264
void function() pure nothrow @nogc @safe 4732BC
void function() pure nothrow @nogc @safe 4732C4

Ali



Re: Feature or bug: print braces

2015-05-14 Thread Alex Parrill via Digitalmars-d-learn

On Thursday, 14 May 2015 at 00:39:25 UTC, Dennis Ritchie wrote:

On Thursday, 14 May 2015 at 00:33:33 UTC, Brian Schott wrote:

You told it to output a function literal, so it did.


Yes, but it would be logical to deduce something like:
-
writeln({}); // prints literal[{}]

Or the compiler will not be able to distinguish the literal 
from the ordinary function arguments?


Literal what?

Associative array? That uses square brackets, not curly brackets.
Struct? What struct would you be creating? And curly braces only 
works for initialization.
Lambda? Well you can omit the parameters if there are none, and 
`{}` in a lambda will give you a block to write, so `{}` is a 
valid lambda that accepts nothing and does nothing.


Re: Feature or bug: print braces

2015-05-14 Thread Dennis Ritchie via Digitalmars-d-learn

On Thursday, 14 May 2015 at 21:55:40 UTC, Alex Parrill wrote:

On Thursday, 14 May 2015 at 00:39:25 UTC, Dennis Ritchie wrote:

On Thursday, 14 May 2015 at 00:33:33 UTC, Brian Schott wrote:

You told it to output a function literal, so it did.


Yes, but it would be logical to deduce something like:
-
writeln({}); // prints literal[{}]

Or the compiler will not be able to distinguish the literal 
from the ordinary function arguments?


Literal what?

Associative array? That uses square brackets, not curly 
brackets.
Struct? What struct would you be creating? And curly braces 
only works for initialization.
Lambda? Well you can omit the parameters if there are none, and 
`{}` in a lambda will give you a block to write, so `{}` is a 
valid lambda that accepts nothing and does nothing.


I just wanted to say that writeln function of demand should not 
print anything else at this challenge, not 804CF88 :)


-
writeln({});


Re: Feature or bug: print braces

2015-05-14 Thread Dennis Ritchie via Digitalmars-d-learn

On Thursday, 14 May 2015 at 22:55:43 UTC, Ali Çehreli wrote:
Yes, it is weird but that value happens to be the address of 
the function. Here is another test:


import std.stdio;

void foo() pure nothrow @nogc @safe
{}

void main()
{
void printInfo(T)(T t)
{
writefln(%s %s, T.stringof, t);
}

auto f = (){};// -- Why the need for () here?

printInfo(foo);
printInfo(f);
printInfo({});// -- No need for () here.
}

There is an inconsistency where a lambda need to be defined 
with empty parentheses in one context while it is just fine 
without in another context.


One output shows that they are all of the same type (function 
pointers):


void function() pure nothrow @nogc @safe 473264
void function() pure nothrow @nogc @safe 4732BC
void function() pure nothrow @nogc @safe 4732C4

Ali


Thanks. This example explains a lot. It's like echoes UFCS :)


Re: Feature or bug: print braces

2015-05-13 Thread Brian Schott via Digitalmars-d-learn

On Thursday, 14 May 2015 at 00:29:06 UTC, Dennis Ritchie wrote:

Why doesn't the compiler produces an error?

-
import std.stdio;

void main() {
writeln({});
}
-
http://ideone.com/qTZCAd


You told it to output a function literal, so it did.

(That or you told it to output a struct literal, but the compiler 
has arbitrarily decided that it's a function literal. This is NOT 
my favorite part of D's grammar.)


Re: Feature or bug: print braces

2015-05-13 Thread Dennis Ritchie via Digitalmars-d-learn
Turns out that I can put into the function writeln almost any 
design language:


-
import std.stdio;

void main() {
writeln( { int n = 5; } );
}
-
http://ideone.com/Rp7gZ2


Re: Feature or bug: print braces

2015-05-13 Thread Dennis Ritchie via Digitalmars-d-learn

On Thursday, 14 May 2015 at 00:33:33 UTC, Brian Schott wrote:

You told it to output a function literal, so it did.


Yes, but it would be logical to deduce something like:
-
writeln({}); // prints literal[{}]

Or the compiler will not be able to distinguish the literal from 
the ordinary function arguments?