Understanding Safety of Function Pointers vs. Addresses of Functions

jmh530 via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sun Jul 12 14:07:32 PDT 2015


On Sunday, 12 July 2015 at 20:31:20 UTC, jmh530 wrote:
> On Sunday, 12 July 2015 at 17:11:04 UTC, anonymous wrote:
>>
>> And personally, I'd probably just type out `x.map!fun.array` 
>> every time.
>>
>> [1] http://dlang.org/hijack.html
>
> Thanks for the comments.

After thinking it over, I think you're absolutely right about the 
function pointers. I made a slight change to just this:

private template givemeabettername(alias fun)
{
     T givemeabettername(T : U[], U)(T x)
         if (isArray!(T))
     {
         return x.map!(a => fun(a)).array;
     }
}

Very cool. I guess I'm still left with the issue that I don't 
know how to loop through a bunch of different functions at 
compile time, but this is so elegant that it doesn't seem like 
that big a deal.

I'll admit, I still don't think I really have a handle on alias 
(i.e., it's not something I immediately think of; I understand 
what it does). It seems like you can do so many things with it. 
It's just not something that I used in other languages much.


More information about the Digitalmars-d-learn mailing list