Thread overview
In the new D release why use free functions instead of properties?
Aug 18, 2014
Gary Willoughby
Aug 18, 2014
Jonathan M Davis
Aug 19, 2014
Idan Arye
Aug 19, 2014
Idan Arye
Aug 19, 2014
Gary Willoughby
Aug 19, 2014
Dicebot
Aug 19, 2014
monarch_dodra
Aug 19, 2014
Jonathan M Davis
August 18, 2014
In the new D release there have been some changes regarding built-in types.

http://dlang.org/changelog.html?2.066#array_and_aa_changes

I would like to learn why this has been done like this and why it is desired to be free functions rather than properties?
August 18, 2014
On Monday, 18 August 2014 at 21:02:09 UTC, Gary Willoughby wrote:
> In the new D release there have been some changes regarding built-in types.
>
> http://dlang.org/changelog.html?2.066#array_and_aa_changes
>
> I would like to learn why this has been done like this and why it is desired to be free functions rather than properties?

Probably because they never should have been properties in the first place. Properties are supposed to emulate variables, whereas something like dup is clearly an action. So, it's clearly not supposed to be a property. However, because D doesn't require parens on a function with no arguments, you can still call it without parens. Some of the changes probably also help with cleaning up the AA internals, which is sorely needed.

- Jonathan M Davis
August 19, 2014
On Monday, 18 August 2014 at 21:17:11 UTC, Jonathan M Davis wrote:
> On Monday, 18 August 2014 at 21:02:09 UTC, Gary Willoughby wrote:
>> In the new D release there have been some changes regarding built-in types.
>>
>> http://dlang.org/changelog.html?2.066#array_and_aa_changes
>>
>> I would like to learn why this has been done like this and why it is desired to be free functions rather than properties?
>
> Probably because they never should have been properties in the first place. Properties are supposed to emulate variables, whereas something like dup is clearly an action. So, it's clearly not supposed to be a property. However, because D doesn't require parens on a function with no arguments, you can still call it without parens. Some of the changes probably also help with cleaning up the AA internals, which is sorely needed.
>
> - Jonathan M Davis

Also std.algorithm's heavy usage of passing delegates as template arguments makes it more elegant to use free functions:

    import std.algorithm;
    import std.compiler;

    void main()
    {
        int[][] arr=[[1,2],[3,4]];
        static if (version_minor<=65){
            auto arr2=arr.map!(a => a.dup)();
        }else{
            auto arr2=arr.map!(a.dup)();
        }
        arr2[0][0]=9;
        assert(arr2[0][0] == 1);
    }
August 19, 2014
On Tuesday, 19 August 2014 at 00:54:25 UTC, Idan Arye wrote:
> On Monday, 18 August 2014 at 21:17:11 UTC, Jonathan M Davis wrote:
>> On Monday, 18 August 2014 at 21:02:09 UTC, Gary Willoughby wrote:
>>> In the new D release there have been some changes regarding built-in types.
>>>
>>> http://dlang.org/changelog.html?2.066#array_and_aa_changes
>>>
>>> I would like to learn why this has been done like this and why it is desired to be free functions rather than properties?
>>
>> Probably because they never should have been properties in the first place. Properties are supposed to emulate variables, whereas something like dup is clearly an action. So, it's clearly not supposed to be a property. However, because D doesn't require parens on a function with no arguments, you can still call it without parens. Some of the changes probably also help with cleaning up the AA internals, which is sorely needed.
>>
>> - Jonathan M Davis
>
> Also std.algorithm's heavy usage of passing delegates as template arguments makes it more elegant to use free functions:
>
>     import std.algorithm;
>     import std.compiler;
>
>     void main()
>     {
>         int[][] arr=[[1,2],[3,4]];
>         static if (version_minor<=65){
>             auto arr2=arr.map!(a => a.dup)();
>         }else{
>             auto arr2=arr.map!(a.dup)();
>         }
>         arr2[0][0]=9;
>         assert(arr2[0][0] == 1);
>     }

Sorry - that should have been:

     import std.algorithm;
     import std.compiler;

     void main()
     {
         int[][] arr=[[1,2],[3,4]];
         static if (version_minor<=65){
             auto arr2=arr.map!(a => a.dup)();
         }else{
             auto arr2=arr.map!(dup)();
         }
         arr2[0][0]=9;
         assert(arr2[0][0] == 1);
     }
August 19, 2014
On Tuesday, 19 August 2014 at 00:55:24 UTC, Idan Arye wrote:
> On Tuesday, 19 August 2014 at 00:54:25 UTC, Idan Arye wrote:
>> On Monday, 18 August 2014 at 21:17:11 UTC, Jonathan M Davis wrote:
>>> On Monday, 18 August 2014 at 21:02:09 UTC, Gary Willoughby wrote:
>>>> In the new D release there have been some changes regarding built-in types.
>>>>
>>>> http://dlang.org/changelog.html?2.066#array_and_aa_changes
>>>>
>>>> I would like to learn why this has been done like this and why it is desired to be free functions rather than properties?
>>>
>>> Probably because they never should have been properties in the first place. Properties are supposed to emulate variables, whereas something like dup is clearly an action. So, it's clearly not supposed to be a property. However, because D doesn't require parens on a function with no arguments, you can still call it without parens. Some of the changes probably also help with cleaning up the AA internals, which is sorely needed.
>>>
>>> - Jonathan M Davis
>>
>> Also std.algorithm's heavy usage of passing delegates as template arguments makes it more elegant to use free functions:
>>
>>    import std.algorithm;
>>    import std.compiler;
>>
>>    void main()
>>    {
>>        int[][] arr=[[1,2],[3,4]];
>>        static if (version_minor<=65){
>>            auto arr2=arr.map!(a => a.dup)();
>>        }else{
>>            auto arr2=arr.map!(a.dup)();
>>        }
>>        arr2[0][0]=9;
>>        assert(arr2[0][0] == 1);
>>    }
>
> Sorry - that should have been:
>
>      import std.algorithm;
>      import std.compiler;
>
>      void main()
>      {
>          int[][] arr=[[1,2],[3,4]];
>          static if (version_minor<=65){
>              auto arr2=arr.map!(a => a.dup)();
>          }else{
>              auto arr2=arr.map!(dup)();
>          }
>          arr2[0][0]=9;
>          assert(arr2[0][0] == 1);
>      }

This kind of makes sense for `dup` because that could be applied across types but what about rehash, byKey, byValue, keys, values, etc of AA's? Surely these will only be used by AA's? Is this more about speed optimisation?
August 19, 2014
On Tuesday, 19 August 2014 at 16:04:15 UTC, Gary Willoughby wrote:
> This kind of makes sense for `dup` because that could be applied across types but what about rehash, byKey, byValue, keys, values, etc of AA's? Surely these will only be used by AA's? Is this more about speed optimisation?

I am pretty sure this is just one of steps for "librarization" of AA implementation and minimizing special handling for it in compiler. Not that valuable on its own, just one of many small steps towards hard goal.
August 19, 2014
On Monday, 18 August 2014 at 21:17:11 UTC, Jonathan M Davis wrote:
> On Monday, 18 August 2014 at 21:02:09 UTC, Gary Willoughby wrote:
>> In the new D release there have been some changes regarding built-in types.
>>
>> http://dlang.org/changelog.html?2.066#array_and_aa_changes
>>
>> I would like to learn why this has been done like this and why it is desired to be free functions rather than properties?
>
> Probably because they never should have been properties in the first place. Properties are supposed to emulate variables, whereas something like dup is clearly an action. So, it's clearly not supposed to be a property. However, because D doesn't require parens on a function with no arguments, you can still call it without parens. Some of the changes probably also help with cleaning up the AA internals, which is sorely needed.
>
> - Jonathan M Davis

Actually, the new free functions *are* properties. All that you just declared is valid, but we never got around to doing it. Walter (If I remember correctly) was opposed.

So right now, even if "dup" is a free function, "myArray.dup()" is still invalid.

:(
August 19, 2014
On Tuesday, 19 August 2014 at 16:28:54 UTC, monarch_dodra wrote:
> Actually, the new free functions *are* properties. All that you just declared is valid, but we never got around to doing it. Walter (If I remember correctly) was opposed.
>
> So right now, even if "dup" is a free function, "myArray.dup()" is still invalid.

Yuck. It shouldn't even be a breaking change to make them functions unless code is specifically testing dup vs dup(). We really should make that change IMHO.

- Jonathan M Davis