Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

This is one reason why I prefer Haskell ;)


I think most statically typed languages don't care for this kind of shenanigans.


Java for example runs arbitrary code when a class is loaded (static {} blocks).

Windows has DllMain, which lets you run code at load of a shared lib and similar things are possible on Linux, using the -init linker flag and friends.

Finally, Haskell has features that allow you to load, compile and run Haskell (through the GHCMonad, if installed), which allows precisely for those shenanigans.

All those lack the habit that the OP is describing (and which broke for him): the use runtime introspection during development time (possibly in a REPL), but that's another pair of shoes.


In addition to the many other languages listed here, Go allows side effects on import.


That's interesting. I can see the motivation... it's helpful to have modules come into the world fully initialized, and initialization often involves side effects.

Is Rust going to allow side effects on initialization?


No. Having module import perform side effects delays program startup unnecessarily and makes the semantics of the program depend on the order in which modules got initialized, which is confusing.


I was referring more to Haskell's lack of side effects. I don't think you can do this in modules.


You can, with unsafePerformIO. Unlike many instances where we can just wave away unsafePerformIO and declare it's an exception that you'll probably never encounter unless you ask for it, this is one instance you may encounter "in the wild" that may not be entirely safe, or may introduce a global scope where you weren't necessarily expecting one. In particular, this usually takes the form of creating a Reference of some kind at initialization time (IORef, STM TVar, etc).




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: