www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Feature or bug: print braces

reply "Dennis Ritchie" <dennis.ritchie mail.ru> writes:
Why doesn't the compiler produces an error?

-----
import std.stdio;

void main() {
	writeln({});
}
-----
http://ideone.com/qTZCAd
May 13 2015
next sibling parent reply "Brian Schott" <briancschott gmail.com> writes:
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.)
May 13 2015
parent reply "Dennis Ritchie" <dennis.ritchie mail.ru> writes:
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?
May 13 2015
parent reply "Alex Parrill" <initrd.gz gmail.com> writes:
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.
May 14 2015
parent reply "Dennis Ritchie" <dennis.ritchie mail.ru> writes:
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({});
May 14 2015
parent reply =?UTF-8?B?QWxpIMOHZWhyZWxp?= <acehreli yahoo.com> writes:
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
May 14 2015
parent "Dennis Ritchie" <dennis.ritchie mail.ru> writes:
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 :)
May 14 2015
prev sibling next sibling parent "Dennis Ritchie" <dennis.ritchie mail.ru> writes:
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
May 13 2015
prev sibling parent reply "Ivan Kazmenko" <gassa mail.ru> writes:
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
May 15 2015
parent "Dennis Ritchie" <dennis.ritchie mail.ru> writes:
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);
May 15 2015