How does calling function pointers work?
helxi
brucewayneshit at gmail.com
Mon Nov 12 16:08:28 UTC 2018
As far as I understand, calling a function pointer with an
argument in D looks like:
call(&fnptr, argTofn0, argTofn1, argTofn3);
This immediately struck me a very weak syntax to me so I decided
to explore my concerns.
I made a function pointer that takes an indefinite number of
arguments.
1 import std.stdio;
2
3 int total(int[] numbers ...) {
4 int result;
5 for(ulong i = 0; i < numbers.length; result +=
numbers[i++]){}
6 return result;
7 }
8
9
10 void main() {
11 writeln(total(1000, 200, 30, 4)); // 1234
12 writeln(&total, 1000, 200, 30, 4); //
55CA386877AC1000200304
13 writeln((&total, 1000, 200, 30), 4); // error lmao
14 }
How do you guys make writeln distinguish between an arg meant for
writeln from an arg meant for &total?
FYI
Line 12 was meant to print 1234.
Line 13 was meant to print 1234 too, but for a different reason.
More information about the Digitalmars-d-learn
mailing list