Jump to page: 1 2
Thread overview
betterC question
Nov 19, 2020
Dibyendu Majumdar
Nov 19, 2020
Adam D. Ruppe
Nov 19, 2020
Dibyendu Majumdar
Nov 19, 2020
rikki cattermole
Nov 19, 2020
Dibyendu Majumdar
Nov 19, 2020
Mike Parker
Nov 19, 2020
Dibyendu Majumdar
Nov 19, 2020
Jacob Carlborg
Nov 19, 2020
Dibyendu Majumdar
Nov 19, 2020
Adam D. Ruppe
Nov 19, 2020
Jack
Nov 19, 2020
Dibyendu Majumdar
Nov 19, 2020
Dibyendu Majumdar
Nov 19, 2020
Basile B.
November 19, 2020
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
November 19, 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.
November 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:
>> 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.


November 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:
>> int function() fp = test;
>
> You want &test to get the address.

Okay that works. Thanks



November 19, 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:
>> 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;
}
November 19, 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.
November 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 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.
November 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 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
November 19, 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:
>> 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.

Well Java and C# have streams and it looks perfectly fine without this kind of syntax.


November 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.
« First   ‹ Prev
1 2