Hacker News new | ask | show | jobs
by WalterBright 1 day ago
For example, C and C++ still cannot compile this:

    int foo() { return bar(); }

    int bar() { return 3; }
3 comments

Because of stuff like ADL, the declaration order matters, thus I don't expect anyone ever submitting a paper to fix that.

And even if that isn't a problem, given how ISO works, someone still needs to get that paper in and voted.

Funny that C89/C90 would compile this specific example just fine due to implicit function declaration feature, which was deprecated in C99.
Is it because bar is defined after foo?
Yes.

It has deleterious consequences. One solution is to add a forward declaration, which is the kind of busywork a language is supposed to eliminate.

Another is to reverse the natural order of functions, with the implementation functions at the top and the interface of the module at the bottom.

After all, do you read a website from top to bottom or bottom to top?

Lua has the same problem with local functions, and Lua was the first language I learned that I still use today. So it feels very natural for me to see local functions with no dependencies at the top, and the ones with the most dependencies at the bottom. (but you can work around this in lua with globals, forward declarations, functions in tables, etc)

So I guess I kind of prefer seeing helper functions at the top and the main logic at the bottom most of the time.

BTW, that code snippet will compile properly with D's ImportC compiler. Proof that it is not impractical.