3 ms·
An ML like notation would be even more cool: char *str[10]; str : [10] (*char) char *(*fp)( int, float *); fp : *((int, *float) -> *char) void (*si
by loup-vaillant 16y ago
An ML like notation would be even more cool:
char *str[10];
str : [10] (*char)
char *(*fp)( int, float *);
fp : *((int, *float) -> *char)
void (*signal(int, void (*fp)(int)))(int);
signal : (int, *(int -> void)) -> *(int -> void)
(Oh. That last declaration did make some sense, after all…)
Really, how did they manage to chose such an inconsistent, unreadable syntax for their declarations? Is there any rational explanation?
- endgame 16y agoAIUI, the explanation is that they wanted this idea of "declaration follows use". For simple things, this works kind of nicely: char *foo; Says that *foo will have type `char`. This sounds good in theory, but famously breaks down when things get more complicated (arrays of function pointers, multiple declarations at once, &c.).
- mturmon 16y agoYes, see van der Linden's Deep C Secrets: http://books.google.com/books?id=4vm2xK3yn34C&printsec=frontcover&dq=deep+c+secrets&source=bl&ots=Hna8W92Id0&sig=G7re-qV-7tRMddDVnG_e-6f0GZ8&hl=en&ei=fcYmTMbeIcf7nAe_2ri8Bg&sa=X&oi=book_result&ct=result&resnum=5&ved=0CCgQ6AEwBA#v=onepage&q&f=false http://books.google.com/books?id=4vm2xK3yn34C&printsec=f...
- jerf 16y agoThis was on HN a bit ago, and you reminded me of it: http://www.csse.monash.edu.au/~damian/papers/HTML/ModestProposal.html http://www.csse.monash.edu.au/~damian/papers/HTML/ModestProp... Basically a fully-fleshed out version of your idea.
- fexl 16y agoThe rationale is that the type declaration demonstrates how you use the variable. For example: char *(*fp)(int, float *) You now have a variable called "fp". Follow that pointer by putting a * in front of it. Call that function by passing it an int and a float pointer inside parentheses. Follow that pointer by putting a * in front of it. That gives you a char. Same kind of thing here: char *strings[10] You now have a variable called "strings". Index that array by putting an offset less than 10 inside square brackets. Follow that pointer by putting a * in front of it. That gives you a char. Here's a simpler example: char *str You now have a variable called "str". Follow that pointer by putting a * in front of it. That gives you a char. Here's the simplest example of all: char ch; You now have a variable called "ch". That gives you a char.