0

I'm currently working on my own programming language. I've started implementing forward references using multiple passes (parsing then analysing, detecting cycles...). The algorithms are a bit heavy but it's all quite fun to use once it's done.

Why are 99% of languages today using forward declarations only? Is it just for the compile times? Is it dogma? Languages stuck in the ancient times? People who make compilers don't know how to implement that? Nobody has tried making a language in the last 10 years?

I'm asking, because I think that forward references are awesome and I believe I should definitely fully integrate them in my language. Is there actually anything wrong with them?

Beyond the compilation, everything is unordered inside a huge virtual memory pool. So I don't see any reason it would be different for the code itself...

unknown
  • 368
  • 4
  • 12
  • 2
    I strongly reject the premise of your question. The only languages I know with forward declarations are C and C++ (and there only in free functions). In all other languages I know (Java, C#, Scala, F#, Haskell, ML, Scala, Prolog, JavaScript, Ruby, Python, Lua, ...) forward declarations aren't a thing. – sepp2k Nov 21 '17 at 21:29
  • 1
    @sepp2k Pascal, and languages based on it (e.g. Delphi), PL/SQL, and possibly other more or less modern languages require it. In your list of languages I see VM's, functional languages and some scripting languages, but nothing with a native compiler. I'm not sure, but maybe there is a relation (those languages being developed in an era where computers had more resources to spend on compiling). – GolezTrol Nov 21 '17 at 21:46
  • Related question with some elaborate answers: [What are forward declarations](https://stackoverflow.com/questions/4757565/what-are-forward-declarations-in-c) – GolezTrol Nov 21 '17 at 21:46
  • 1
    @GolezTrol Some of the functional languages I listed *are* natively compiled (at least Haskell and ML are). Rust and Swift are two examples of natively compiled languages that aren't primarily functional. I'd definitely agree that the difference is related to the era (the languages you mentioned are quite old as well and I can't think of a single language invented in the 90s or later that has forward declarations). Either way I'd say that "99% of languages today" is far from an accurate assessment. – sepp2k Nov 21 '17 at 21:53
  • 2
    I think your 99% claim is wrong. Most languages younger than 20 years (C#, Java, Swift, Scala, etc.) all are perfectly fine with forward references. It's really only the few (still very popular languages) from the age of single-pass compilers that require explicit forward declarations: C, C++, Pascal. – munificent Nov 21 '17 at 23:47
  • @sepp2k - Clojure needs forward declarations. – Frank C. Nov 22 '17 at 12:32
  • @FrankC. Interesting. I did not know that. – sepp2k Nov 22 '17 at 12:50

2 Answers2

3

“Programs must be written for people to read, and only incidentally for machines to execute.”1

One of the hardest part of reading / reviewing code is figuring out where is this name defined and how is it used. That's why every modern convention suggests defining variables as close to their usage as possible, that's why C's model of declaring all variables at the start of the function has been completely abandoned in other languages. That's why most C++ code has the weird Hungarian-like convention of adding the m_ prefix before the names of all members and Python has the __ prefix. Adding forward references only adds one more place for sloppy people to define their variables - after their usage, potentially hundreds of lines after.

Forward references don't add anything interesting as all code relying on them can be equivalently rewritten, clutter code and make it harder to read and, on top of all of that, make compilers harder to write. Sure, it's not an issue with your homegrown language but try adding that feature to a context-sensitive language like C++.

The value to cost ratio of that feature is simply not worth the implementation. Heck, even if implementing it was free, I'd still tell you that the benefits won't be a sufficient reason to put them in a language.

Nikola Dimitroff
  • 6,127
  • 2
  • 25
  • 31
2

You're close to having the answer: historically many languages were developed prior to having huge VM pools, and also on machines with slow CPUs.

So using forward declarations allowed a smaller number of passes over the source code, since you were guaranteed that any declaration you needed would have been processed already.

Mark Harrison
  • 297,451
  • 125
  • 333
  • 465