digitalmars.D.learn - What does alias do?
- xtreak (14/14) Apr 23 2016 I am a D newbie from Python and I am trying to grok alias. Is
- xtreak (4/18) Apr 23 2016 Adam D Ruppe was helping me on #d . I just thought posting to
- Adam D. Ruppe (4/6) Apr 23 2016 There's no problem with posting to the forums and talking on IRC.
- ag0aep6g (9/21) Apr 23 2016 My Python isn't too great, but I think this is more similar to function
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 Renaming imported function from itertools import permutations as p p([1, 2], 2) 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])?
Apr 23 2016
On Saturday, 23 April 2016 at 19:49:46 UTC, 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 Renaming imported function from itertools import permutations as p p([1, 2], 2) 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])?Adam D Ruppe was helping me on #d . I just thought posting to forum will have more people to discuss on this. Sorry for the double post.
Apr 23 2016
On Saturday, 23 April 2016 at 19:52:15 UTC, xtreak wrote:I just thought posting to forum will have more people to discuss on this. Sorry for the double post.There's no problem with posting to the forums and talking on IRC. Since the chat isn't searchable and disappears fast, the forums lets a lot more people get involved to read and write.
Apr 23 2016
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.myextendMy 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`.
Apr 23 2016
On Saturday, 23 April 2016 at 20:01:00 UTC, ag0aep6g wrote:On 23.04.2016 21:49, xtreak wrote: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 preferredI 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.myextendMy 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`.
Apr 27 2016
On 27.04.2016 21:40, xtreak wrote: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.As far as I understand, you can imagine `z => z * z` being pasted for `f` in the alias version. That is, for `test!(z => z * z)(10)` a function `int test(int num) {return (z => z * z)(num);}` is generated. It should be easy for the compiler/optimizer to turn that into `return z * z;`. When `f` is a function parameter, a function pointer / delegate is passed at run-time, unless the optimizer manages to inline it. I don't know how good/aggressive inliners are and if they fail to do this in practice. Also note that the alias version generates a new function for every f. The function parameter version only generates one function per `typeof(f)`.Also the second form of short notation throws an error that it returns void.This line, right? writeln(test1(10, ((z) => z *z))); That doesn't work because both the type of test1's V and the type of z are generic. For example, z could be int or float, and V could be `int function(int)` or `float function(float)`. The compiler can't decide that, so it errors out. Either z or the type of f need to be more explicit. These are ok: ---- writeln(test1(10, (int z) => z * z)); /* now V can be deduced as int function(int) */ T test2(T)(T num, T function(T) f){ /* building f's type from T */ return f(num); } writeln(test2!int(10, z => z * z)); /* giving T explicitly */ ---- The alias version doesn't have this problem because `z => z * z` is just pasted in there. z's type doesn't matter until the call is analyzed. And then num's type is known which forces z's type.
Apr 27 2016