4 ms·
I've heard this phrase - "declarations mirror use" many times before but it just doesn't click for me for some reason. Who's use? The compiler's or the program
by bogomipz 10y ago
I've heard this phrase - "declarations mirror use" many times before but it just doesn't click for me for some reason.
Who's use? The compiler's or the programmer's? Can you elaborate? Sorry if this silly question but I've scratched my head enough on hearing that phrase that I thought I would ask. Cheers.
- bluetomcat 10y agoThe meaning of "declarations mirror use" is that the programmer can use the declared variable in the exact same way as being declared (by applying the exact same operators): int **p; // declares "p" as a pointer to a pointer to int int x = **p; // the "**p" expression mirrors the declaration and its type is "int" This is true even for functions: int *f(int a); // declares a function which takes an int and returns a pointer to int int res = *f(3); // the "*f(3)" expression mirrors the declaration and its type is "int" Arrays are obvious when looked that way: int arr[5][5]; int elem = arr[1][2]; // the "arr[1][2]" expression mirrors the declaration
- bogomipz 10y agoThanks for the clear explanation. This makes sense. I do have a tangential question. I have read that the reason for this declaration syntax is that it was easier form compilers and compiler writers to parse. How does this declaration syntax facilitate parsing exactly? I fail to see why this is.
- bluetomcat 10y agoThe advantage is that you don't introduce any new operators or syntactic forms which would only be used in declarations. This means a smaller set of tokens and a smaller set of syntactic forms. Being able to parse an expression like (*arr[i])(42) means that you can use pretty much the same machinery to parse int (*arr[SIZE])(int a)
- bogomipz 10y agoThis is awesome. Thank you.