digitalmars.D.learn - betterC question
- Dibyendu Majumdar (15/15) Nov 18 2020 I have simple test program:
- Adam D. Ruppe (5/6) Nov 18 2020 This tries to *call* the function test and assign its return
- Dibyendu Majumdar (3/9) Nov 18 2020 Really? why does it do that?
- rikki cattermole (9/17) Nov 18 2020 You don't need the brackets to call a function (and with a little help
- Dibyendu Majumdar (3/11) Nov 18 2020 Okay thanks. Bad idea IMO.
- Mike Parker (4/17) Nov 18 2020 Imagine what range pipelines would look like without it. This is
- Dibyendu Majumdar (3/22) Nov 19 2020 Well Java and C# have streams and it looks perfectly fine without
- Jacob Carlborg (34/46) Nov 19 2020 Yes, calling `writeln` like that is a bad idea. That was a bad
- Dibyendu Majumdar (4/10) Nov 19 2020 I think that properties on an object are a special case - but
- Adam D. Ruppe (4/5) Nov 19 2020 That's kinda how I see C taking the address of various things
- Jack (3/8) Nov 19 2020 good example
- Dibyendu Majumdar (3/8) Nov 19 2020 To be honest it seems irrelevant what C does.
- Dibyendu Majumdar (3/7) Nov 18 2020 Okay that works. Thanks
- Basile B. (12/27) Nov 19 2020 IMO another problem here is that `function` and `delegate` are
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
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
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:Really? why does it do that?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
On 19/11/2020 1:11 PM, Dibyendu Majumdar wrote:On Thursday, 19 November 2020 at 00:08:59 UTC, Adam D. Ruppe 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; }On Thursday, 19 November 2020 at 00:07:12 UTC, Dibyendu Majumdar wrote:Really? why does it do that?int function() fp = test;This tries to *call* the function test and assign its return value to fp.
Nov 18 2020
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
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:Imagine what range pipelines would look like without it. This is one of my favorite D features.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
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:this kind of syntax.On Thursday, 19 November 2020 at 00:18:54 UTC, rikki cattermole wrote:Imagine what range pipelines would look like without it. This is one of my favorite D features.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 19 2020
On Thursday, 19 November 2020 at 00:20:50 UTC, Dibyendu Majumdar wrote:On Thursday, 19 November 2020 at 00:18:54 UTC, rikki cattermoleYes, 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 CarlborgYou 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 19 2020
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
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
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:good exampleOkay thanks. Bad idea IMO.That's kinda how I see C taking the address of various things implicitly.
Nov 19 2020
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:To be honest it seems irrelevant what C does.Okay thanks. Bad idea IMO.That's kinda how I see C taking the address of various things implicitly.
Nov 19 2020
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:Okay that works. Thanksint function() fp = test;You want &test to get the address.
Nov 18 2020
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 WindowsIMO 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