9 ms·
It's because the C compiler was designed to be single pass, so as to be speedy and require less memory on the systems of it's time.
by rezonant 3y ago
It's because the C compiler was designed to be single pass, so as to be speedy and require less memory on the systems of it's time.
- billfruit 3y agoIs that a requirement that is relevant presently? I would say not so much. Do modern C compilers also make only a single pass?
- hulitu 3y agoYes. Gcc throws errors like hell with c files which have the definitions after functions.
- rezonant 3y agoModern compilers still have to follow the rules of the language.
- billfruit 3y agoYes, but is the constraint that compiler shall make only a single pass, a rule of the language?
- trealira 3y agoPrior to C99 (i.e., in C90), there was a rule that any undeclared identifier id that was followed by a function call parenthesis would be treated as though there were an implicit declaration in the scope in which it was used like this: extern int id(); The empty parentheses meant it could take any number of arguments, since it was declared in the traditional style, where the argument types weren't specified first. This implicit declaration meant that if it was later defined in the same file as returning a type other than int, the compiler wasn't permitted to go back up in the file and treat the function call as though it returned a type other than int. This requirement was removed in C99, but in practice, compilers kept doing it for backwards compatibility, even when not invoked in C90 mode.
- fuzztester 3y agoLike early Pascal compilers, or maybe all of them. Don't know about the latter case (all).
- dheera 3y agoI suppose you could just create a ".d" file standard that doesn't have that requirement but processes into a ".c" file that has the necessary prototypes. You could probably also auto-insert all the #if'n'def nonsense automatically and skip it in the ".d" files. Kind of like how the JavaScript dudes all use this ".ts" stuff now for convenience that processes into ".js"
- foodevl 3y agoThe Arduino build system does this (preprocesses your source code to pull out prototypes and put them at the top). To make things easier for beginners.
- phatskat 3y agoJust to be a little picky, but if you want “convenient” then just stick with pure JS - it’s overly forgiving and simple. TypeScript is lovely and I much prefer it over JS, but having to type _everything_ is far from convenient imo
- vinceguidry 3y agoIsn't Typescript gradually typed?
- phatskat 3y agoIt is! I’ve been working in an environment that essentially requires us to type as we code (rules against using the “any” and “unknown” types) that in just used to them being enforced now lol. So I suppose my point is moot, as the tediousness isn’t forced by the language necessarily.
- rezonant 3y agoIt's also considered by many to be an antipattern to add a type that is otherwise inferred. For a trivial example, let x: number = 6; You should instead allow the compiler to infer where it can and do let x = 6; Inference doesn't work across function calls like it does (did?) in Flow, which is a good thing, so those always need to be typed. If you wanted the type of x to be 6 instead of number, you would use let x = 6 as const;