www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - betterC question

reply Dibyendu Majumdar <d.majumdar gmail.com> writes:
I have simple test program:

import core.stdc.stdio : printf;

void test() {
     int* a;
     printf("a == null %d\n", a == null);
}

int function() fp = test;

extern (C) void main() {
     fp();
}

Why do I get:

\d\dmd-2.092.1\windows\bin64\dmd.exe -betterC tests.d
tests.d(5): Error: printf cannot be interpreted at compile time, 
because it has no available source code

This is on Windows
Nov 18 2020
next sibling parent reply Adam D. Ruppe <destructionator gmail.com> writes:
On Thursday, 19 November 2020 at 00:07:12 UTC, Dibyendu Majumdar 
wrote:
 int function() fp = test;
This tries to *call* the function test and assign its return value to fp. You want &test to get the address.
Nov 18 2020
next sibling parent reply Dibyendu Majumdar <d.majumdar gmail.com> writes:
On Thursday, 19 November 2020 at 00:08:59 UTC, Adam D. Ruppe 
wrote:
 On Thursday, 19 November 2020 at 00:07:12 UTC, Dibyendu 
 Majumdar wrote:
 int function() fp = test;
This tries to *call* the function test and assign its return value to fp.
Really? why does it do that?
 You want &test to get the address.
Nov 18 2020
parent reply rikki cattermole <rikki cattermole.co.nz> writes:
On 19/11/2020 1:11 PM, Dibyendu Majumdar wrote:
 On Thursday, 19 November 2020 at 00:08:59 UTC, Adam D. Ruppe wrote:
 On Thursday, 19 November 2020 at 00:07:12 UTC, Dibyendu Majumdar wrote:
 int function() fp = test;
This tries to *call* the function test and assign its return value to fp.
Really? why does it do that?
You don't need the brackets to call a function (and with a little help from UFCS): void main() { import std.stdio; "Hello!".writeln; writeln; }
Nov 18 2020
parent reply Dibyendu Majumdar <d.majumdar gmail.com> writes:
On Thursday, 19 November 2020 at 00:18:54 UTC, rikki cattermole 
wrote:

 You don't need the brackets to call a function (and with a 
 little help from UFCS):

 void main() {
 	import std.stdio;
 	
 	"Hello!".writeln;
 	writeln;
 }
Okay thanks. Bad idea IMO.
Nov 18 2020
next sibling parent reply Mike Parker <aldacron gmail.com> writes:
On Thursday, 19 November 2020 at 00:20:50 UTC, Dibyendu Majumdar 
wrote:
 On Thursday, 19 November 2020 at 00:18:54 UTC, rikki cattermole 
 wrote:

 You don't need the brackets to call a function (and with a 
 little help from UFCS):

 void main() {
 	import std.stdio;
 	
 	"Hello!".writeln;
 	writeln;
 }
Okay thanks. Bad idea IMO.
Imagine what range pipelines would look like without it. This is one of my favorite D features.
Nov 18 2020
parent Dibyendu Majumdar <d.majumdar gmail.com> writes:
On Thursday, 19 November 2020 at 01:42:16 UTC, Mike Parker wrote:
 On Thursday, 19 November 2020 at 00:20:50 UTC, Dibyendu 
 Majumdar wrote:
 On Thursday, 19 November 2020 at 00:18:54 UTC, rikki 
 cattermole wrote:

 You don't need the brackets to call a function (and with a 
 little help from UFCS):

 void main() {
 	import std.stdio;
 	
 	"Hello!".writeln;
 	writeln;
 }
Okay thanks. Bad idea IMO.
Imagine what range pipelines would look like without it. This is one of my favorite D features.
this kind of syntax.
Nov 19 2020
prev sibling next sibling parent reply Jacob Carlborg <doob me.com> writes:
On Thursday, 19 November 2020 at 00:20:50 UTC, Dibyendu Majumdar 
wrote:
 On Thursday, 19 November 2020 at 00:18:54 UTC, rikki cattermole
 You don't need the brackets to call a function (and with a 
 little help from UFCS):

 void main() {
 	import std.stdio;
 	
 	"Hello!".writeln;
 	writeln;
 }
Okay thanks. Bad idea IMO.
Yes, calling `writeln` like that is a bad idea. That was a bad example. But the actual reason is, this is how D implements properties [1]. Any function that doesn't take an argument can be called without parentheses. Any function which takes a single argument can be called like setting a field. Here's an example: struct Color { private uint hex; int red() out(result; result >= 0 && result <= 255) // assert that the result is within bounds { return (hex & 0xFF0000) >> 16; } void red(int value) in(value >= 0 && value <= 255) // assert that the value is within bounds { hex = (hex & 0x00FFFF) | (value << 16); } // similar functions for green and blue } void main() { Color color; color.red = 255; assert(color.red == 255); } [1] https://en.wikipedia.org/wiki/Property_(programming) -- /Jacob Carlborg
Nov 19 2020
parent Dibyendu Majumdar <d.majumdar gmail.com> writes:
On Thursday, 19 November 2020 at 09:23:25 UTC, Jacob Carlborg 
wrote:
 Yes, calling `writeln` like that is a bad idea. That was a bad 
 example.

 But the actual reason is, this is how D implements properties 
 [1]. Any function that doesn't take an argument can be called 
 without parentheses. Any function which takes a single argument 
 can be called like setting a field.
I think that properties on an object are a special case - but treating an random function identifier as callable is still bad.
Nov 19 2020
prev sibling parent reply Adam D. Ruppe <destructionator gmail.com> writes:
On Thursday, 19 November 2020 at 00:20:50 UTC, Dibyendu Majumdar 
wrote:
 Okay thanks. Bad idea IMO.
That's kinda how I see C taking the address of various things implicitly.
Nov 19 2020
next sibling parent Jack <jckj33 gmail.com> writes:
On Thursday, 19 November 2020 at 14:34:38 UTC, Adam D. Ruppe 
wrote:
 On Thursday, 19 November 2020 at 00:20:50 UTC, Dibyendu 
 Majumdar wrote:
 Okay thanks. Bad idea IMO.
That's kinda how I see C taking the address of various things implicitly.
good example
Nov 19 2020
prev sibling parent Dibyendu Majumdar <d.majumdar gmail.com> writes:
On Thursday, 19 November 2020 at 14:34:38 UTC, Adam D. Ruppe 
wrote:
 On Thursday, 19 November 2020 at 00:20:50 UTC, Dibyendu 
 Majumdar wrote:
 Okay thanks. Bad idea IMO.
That's kinda how I see C taking the address of various things implicitly.
To be honest it seems irrelevant what C does.
Nov 19 2020
prev sibling parent Dibyendu Majumdar <d.majumdar gmail.com> writes:
On Thursday, 19 November 2020 at 00:08:59 UTC, Adam D. Ruppe 
wrote:
 On Thursday, 19 November 2020 at 00:07:12 UTC, Dibyendu 
 Majumdar wrote:
 int function() fp = test;
You want &test to get the address.
Okay that works. Thanks
Nov 18 2020
prev sibling parent Basile B. <b2.temp gmx.com> writes:
On Thursday, 19 November 2020 at 00:07:12 UTC, Dibyendu Majumdar 
wrote:
 I have simple test program:

 import core.stdc.stdio : printf;

 void test() {
     int* a;
     printf("a == null %d\n", a == null);
 }

 int function() fp = test;

 extern (C) void main() {
     fp();
 }

 Why do I get:

 \d\dmd-2.092.1\windows\bin64\dmd.exe -betterC tests.d
 tests.d(5): Error: printf cannot be interpreted at compile 
 time, because it has no available source code

 This is on Windows
IMO another problem here is that `function` and `delegate` are special cases of the `*` postfix. With a syntax like int()* fp = test it would be more obvious to new comers that `&` is missing. This is a syntax I experiment in STYX for example [1]. Note that then there's also the problem with functions pointers requiring a context. This context is not necessarily a `this` (for closures it's a frame obviously). [1] https://gitlab.com/basile.b/styx/-/blob/master/tests/backend/function_pointers.sx#L10
Nov 19 2020