4 ms·
> the compiler only knows about what lexically precedes it. […] it drives programmers to lay out the declarations backwards. The leaf functions come first, and
by pansa2 2y ago
> the compiler only knows about what lexically precedes it. […] it drives programmers to lay out the declarations backwards. The leaf functions come first, and the global interface functions are last. It’s like reading a newspaper article from the bottom up. It makes no sense.
Defining functions on a “bottom-up” order like this is common even in languages like Python which allow forward references. [0]
Is that just a holdover from languages which don’t allow such references? Or does it actually make more sense for certain types of code?
[0] https://stackoverflow.com/a/73131538 https://stackoverflow.com/a/73131538
- almostgotcaught 2y agoYou're confused. There are no forward references in python. It's simply that identifiers in the body of a function aren't resolved until the function itself is executed (if ever) and at the point everything in the module scope has been defined. You can test this yourself by just putting any name into a body and loading the module.
- pansa2 2y ago> identifiers in the body of a function aren't resolved until the function itself is executed (if ever) and at the point everything in the module scope has been defined Yes, so within a function you can refer to things that are defined later in the module. Isn't that's a "forward reference", even if the details are slightly different from how they work in D?
- almostgotcaught 2y ago[flagged]
- pansa2 2y agoYou're right, it's not the same mechanism. But in this particular context it has the same effect - it allows you to define functions in a source file in arbitrary order.
- throwuxiytayq 2y agoIn my code, the public interface always tends to bubble upwards, and implementation details go at the end of the file. I don’t even have a strict rule for this, it just reads more cleanly and seems to make sense. Especially if the implementation is large - you don’t want to scroll through all that before you get to the basics of what it all does. I’m curious how everyone else does things.