What does alias do?

xtreak via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Wed Apr 27 12:40:30 PDT 2016


On Saturday, 23 April 2016 at 20:01:00 UTC, ag0aep6g wrote:
> On 23.04.2016 21:49, xtreak wrote:
>> I am a D newbie from Python and I am trying to grok alias. Is 
>> alias like
>> Python does as below
>>
>> L = []
>> myextend = L.extend
>> L.myextend
>
> My Python isn't too great, but I think this is more similar to 
> function pointers or delegates in D.
>
>> Renaming imported function
>>
>> from itertools import permutations as p
>> p([1, 2], 2)
>
> Yes, that's similar. A renamed import creates an alias. For 
> example, `import std.algorithm: p = permutations;` creates an 
> alias `p` for std.algorithm.permutations.
>
>> Is D aliasing the same as above? How does aliasing types help 
>> like below
>>
>> alias intList = LinkedList!int
>>
>> Is the above like a partially applied template as in 
>> LinkedList!int([1,
>> 2, 3]) and hence can I use it like intList([1, 2, 3])?
>
> No, the template isn't partially applied, it's fully 
> instantiated (and results in a type). The alias declaration 
> just makes `intList` an alternative name for `LinkedList!int`.

import std.array;
import std.range;
import std.algorithm;
import std.stdio;

T test(alias f, T)(T num) {
   return f(num);
}

T test1(T, V)(T num, V f){
     return f(num);
}

void main() {
   writeln("hello world");
   writeln(10000.iota
           .map!(a => a * a)
           .take(5));
   writeln(test!(z => z * z)(10));
   writeln(test1(10, ((z) => z *z)));
   writeln(test1(10, function int(int z) { return z * z; }));
}

What is the difference between passing as alias and then passing 
it as lambda. Does it involve any cost. Also the second form of 
short notation throws an error that it returns void. Kindly help 
me on this as now alias is not pointing to a named symbol so is 
there any cost and why alias is preferred



More information about the Digitalmars-d-learn mailing list