4 ms·
> it seems pretty common for dynamically typed languages and pretty much entirely absent from statically typed ones Counter-example is Go and init() function.
by kokada 4mo ago
> it seems pretty common for dynamically typed languages and pretty much entirely absent from statically typed ones
Counter-example is Go and init() function.
- saghm 4mo agoInteresting, I had no idea that existed! I still think there's a a difference between "here's a hook you can use to run stuff earlier" and "importing any module is fundamentally the same as running it as a script unless the module happens to use a special conditional to wrap stuff inside of" though (and I say this as someone who doesn't go out of his way to defend Go design decisions)
- assbuttbuttass 4mo agoAlso C++/Java static initialization, C# static constructors, or Rust global variable initialization, ... Most languages have this feature Afaik
- ameliaquining 4mo agoRust doesn't have this behavior (sometimes called "life before main"). Code to initialize a static variable runs either at compile time, or lazily on first access, depending on which mechanism you use.
- saghm 4mo agoYeah, I don't think that "precompute something at compile-time" is really comparable to "every import literally executes code as a script". Rust imports are actually about as far from this as I can imagine, because modules can circularly reference each other, which unless I'm misunderstanding would be an infinite loop in Python without manually breaking the chain with some form of conditional. I'm actually kind of surprised to see comments like that one, because compile-time logic feels like the opposite end of the spectrum from what Python imports do, with "regular" code being compiled without any precomputation sitting somewhere in the middle. It seems like I didn't articulate my thoughts clearly enough though, since several people seemed to read what I was saying as being comparable.
- assbuttbuttass 4mo agoInteresting, thanks
- lanstin 4mo agoStatic initializers in C++ - sometime ago I saw savings of some 400 ms (?) startup cost of initializing static strings from constants by moving it to some compile time thing.
- saghm 4mo agoRight; the issue is that this isn't happening at compile time in Python, because it's not getting compiled ahead-of-time. The equivalent would be if header files had imperative code that got executed at runtime in places where they're included. (To preempt potential pedantry: yes, I know that you can compile Python to bytecode ahead of time, but that's not really relevant to what's being discussed here because it doesn't mean "the stuff happening in modules I import isn't happening at runtime anymore")
- thaumasiotes 4mo ago> the issue is that this isn't happening at compile time in Python, because it's not getting compiled ahead-of-time What are those .pyc files for?
- ameliaquining 4mo agoThat was the pedantry that they were trying to preempt.