Most active commenters
  • dvt(7)
  • eru(4)
  • JoshTriplett(3)
  • kketch(3)

←back to thread

517 points bkolobara | 38 comments | | HN request time: 0.001s | source | bottom
Show context
merdaverse ◴[] No.45043051[source]
Code written below your line gets executed if you don't return early. More breaking news at 8.

Seriously, why would you think that assigning a value would stop your script from executing? Maybe the Typescript example is missing some context, but it seems like such a weird case to present as a "data race".

replies(8): >>45043245 #>>45043339 #>>45043398 #>>45043537 #>>45043876 #>>45046033 #>>45046975 #>>45049155 #
lights0123 ◴[] No.45043339[source]
exit(), execve(), and friends do immediately stop execution—I could understand why you'd think a redirect would as well.
replies(4): >>45043409 #>>45043410 #>>45049020 #>>45049406 #
1. dvt ◴[] No.45043410[source]
The redirect is an assignment. In no language has a variable assignment ever stopped execution.
replies(6): >>45043422 #>>45043544 #>>45043663 #>>45043805 #>>45047504 #>>45047601 #
2. dminik ◴[] No.45043422[source]
That doesn't seem that obvious to me. You could have a setter that just calls exit and terminates the whole program.
replies(1): >>45043543 #
3. dvt ◴[] No.45043543[source]
Yeah, this is actually a good point, could have a custom setter theoretically that simply looks like assignment, but does some fancy logic.

    const location = {
      set current(where) {
        if (where == "boom") {
            throw new Error("Uh oh"); // Control flow breaks here
        }
      }
    };

    location.current = "boom" // exits control flow, though it looks like assignment, JS is dumb lol
4. JoshTriplett ◴[] No.45043544[source]

    $ python3
    Python 3.13.7 (main, Aug 20 2025, 22:17:40) [GCC 14.3.0] on linux
    Type "help", "copyright", "credits" or "license" for more information.
    >>> class MagicRedirect:
    ...     def __setattr__(self, name, value):
    ...         if name == "href":
    ...             print(f"Redirecting to {value}")
    ...             exit()
    ... 
    >>> location = MagicRedirect()
    >>> location.href = "https://example.org/"
    Redirecting to https://example.org/
    $
replies(1): >>45045418 #
5. lock1 ◴[] No.45043663[source]
You could overload operator=() in C++ with a call to exit(), which fulfills "variable assignment that halts the program".
replies(3): >>45043699 #>>45045377 #>>45050093 #
6. dvt ◴[] No.45043699[source]
I was ignoring these kinds of fancy overload cases, but even in JS you can mess with setters to get some unexpected behavior (code below).
7. ordu ◴[] No.45043805[source]
Try this in C:

*(int*)0 = 0;

Modern C compilers could require you to complicate this enough to confuse them, because their approach to UB is weird, if they saw an UB they could do anything. But in olden days such an assignment led consistently to SIGSEGV and a program termination.

replies(1): >>45043955 #
8. DannyBee ◴[] No.45043955[source]
Unless you were on systems that mapped address 0 to a writable but always zero value so they could do load and store speculation without worry.

IBM did this for a long time

replies(3): >>45044327 #>>45045931 #>>45051939 #
9. ◴[] No.45044327{3}[source]
10. BobbyJo ◴[] No.45045377[source]
idk if I'd consider overloading the assignment operator to call a function, then using it, actually an assignment in truth.
replies(1): >>45048519 #
11. dvt ◴[] No.45045418[source]
You're overloading a setter here. It's cute, I did it in JS as well, but I don't really think it's a counterexample. It would be odd to consider this the norm (per the thought process of the original blog post).
replies(2): >>45045557 #>>45045946 #
12. rowanG077 ◴[] No.45045557{3}[source]
This is not some weird thing. Here is a run of the mill example where python can have properties settting do anything at all. And it's designed like that.

    import sys
    
    class Foo:
        @property
        def bar(self):
            return 10
            
        @bar.setter
        def bar(self, value):
            print("bye")
            sys.exit()
    
    foo = Foo()
    foo.bar = 10
Or in C# if you disqualify dynamic languages:

    using System;

    class Foo
    {
        public int Bar
        {
            get { return 10; }
            set
            {
                Console.WriteLine("bye.");
                Environment.Exit(0);
            }
        }
    }

    class Program
    {
        static void Main()
        {
            Foo obj = new Foo();
            obj.Bar = 10;
        }
    }

This is not some esoteric thing in a lot of programming languages.
replies(1): >>45046687 #
13. mabster ◴[] No.45045931{3}[source]
My favourite were older embedded systems where 0 was an address you actually do interact with. So for some portion of the code you WANT null pointer access. I can't remember the details but I do remember jumping to null to reset the system being pretty common.
replies(2): >>45046092 #>>45049285 #
14. dminik ◴[] No.45045946{3}[source]
But window.location.href is already an overloaded setter. It schedules a page navigation.
15. marshray ◴[] No.45046092{4}[source]
Probably the system interrupt table. Index 0 might reference the handler for the non-maskable interrupt NMI, often the same as a power-on reset.

I recall that on DOS, Borland Turbo C would detect writes to address 0 and print a message during normal program exit.

16. dvt ◴[] No.45046687{4}[source]
You're also overriding a setter. Maybe I'm going against the grain here, but it's absolutely esoteric. The assignment operator is not supposed to have side-effects, and maybe this is the logician in me, but the implication that we should be aware that weird stuff might be happening when we do `x = 5` is fundamentally bonkers.
replies(4): >>45046718 #>>45047382 #>>45048993 #>>45050028 #
17. JoshTriplett ◴[] No.45046718{5}[source]
You started with "In no language has a variable assignment ever stopped execution", and now you're saying "The assignment operator is not supposed to have side-effects". location.href is a counterexample, and there are many counterexamples throughout various tools and languages and libraries. Deciding how you think things should work does not affect how things do work, and it's important to understand the latter. (I do agree it's bad practice, but it happens and people do not always fully control the environments they must work with.)

And given that location.href does have a side effect, it's not unreasonable for someone to have assumed that that side effect was immediate rather than asynchronous.

That said, if you don't like working with such languages, that's all the more reason to select languages where that doesn't happen, which comes back to the point made in the article.

replies(1): >>45046819 #
18. dvt ◴[] No.45046819{6}[source]
> You started with "In no language has a variable assignment ever stopped execution",

The irony is that I'm still technically correct, as literally every example (from C++, to C#, to Python, to JS) have been object property assignments abusing getters and setters—decidedly not variable assignments (except for the UB example).

replies(2): >>45046918 #>>45047457 #
19. rowanG077 ◴[] No.45046918{7}[source]
The entire discussion is about a property assignment. Which in colloquial usage is also called variable assignment. Which is obvious since nobody corrected you on that. You now trying to do a switcheroo is honestly ridiculous.
replies(1): >>45047059 #
20. dvt ◴[] No.45047059{8}[source]
The entire discussion is about “=“ doing weird stuff, which in 99.9% of cases it does not do. And my point was that no language, without doing weird stuff (like overloading), does not let “=“ do weird stuff (and thus is pure). The counterarguments all involve nonstandard contracts. Therefore, thinking that using “=“ will have some magical side-effect is absolutely never expected by default.
replies(2): >>45047506 #>>45048333 #
21. kketch ◴[] No.45047382{5}[source]
assignments are side effects, even more so when they are done through a setter on an object / class instance
22. kketch ◴[] No.45047457{7}[source]
Technically no. Producing side effects from a setter is not unexpected, even if it often the best idea to have a setter have a lot of unexpected side effects. However producing side effects from getters is definitely unexpected and should not be done. Interestingly it's one of the areas where rust is really useful, it forces you express your intent in terms of mutability and is able to enforce these expectations we have.
replies(2): >>45047582 #>>45048499 #
23. AdieuToLogic ◴[] No.45047504[source]
> The redirect is an assignment. In no language has a variable assignment ever stopped execution.

Many languages support property assignment semantics which are defined in terms of a method invocation. In these languages, the method invoked can stop program execution if the runtime environment allows it to do so.

For example, source which is defined thusly:

  foo.bar = someValue
Is evaluated as the equivalent of:

  foo.setBar (someValue)
24. JoshTriplett ◴[] No.45047506{9}[source]
> The counterarguments all involve nonstandard contracts. Therefore, thinking that using “=“ will have some magical side-effect is absolutely never expected by default.

That sounds like a recipe for having problems every time you encounter a nonstandard contract. Are you actually saying you willfully decide never to account for the possibility, or are you conflating "ought not to be" with "isn't"?

If I'm programming in a language that has the possibility of properties, it's absolutely a potential expectation at any time. Which is one reason I don't enjoy programming in such languages as much.

To give a comparable example: if I'm coding in C, "this function might actually be a macro" is always a possibility to be on guard against, if you do anything that could care about the difference (e.g. passing the function's name as a function pointer).

25. galangalalgol ◴[] No.45047582{8}[source]
Overloading assignment operators to maintain an invariant is one thing, but this particular case of it running off and effectively doing ionis weird to me coming from an embedded c++ background. I don't like operator overloading and think it should be avoided, just to make my bias obvious. I don't code c++ anymore either, rust and no looking back for a few years now.
replies(1): >>45050085 #
26. kji ◴[] No.45047601[source]
In Blink setHref is automatically bound to C++ code [1]. I think it's fair to say that anything goes.

[1]: https://source.chromium.org/chromium/chromium/src/+/main:thi...

27. scheme271 ◴[] No.45048333{9}[source]
So, with anything that isn't a primitive type (e.g. int, bool, etc), there's a chance that assignment is going to require memory allocation or something similar. If that's the case then there's a chance of bad things happening (e.g. a out of memory error and the program being killed).

More commonly, if you look at things like c++'s unique_ptr, assignment will do a lot of things in the background in order to keep the unique_ptr properties consistent. Rust and other languages probably do similar things with certain types due to semantic guarantees.

replies(1): >>45048505 #
28. eru ◴[] No.45048499{8}[source]
> Interestingly it's one of the areas where rust is really useful, it forces you express your intent in terms of mutability and is able to enforce these expectations we have.

Though Rust only cares about mutability, it doesn't track whether you are going to launch the nukes or format the hard disk.

replies(1): >>45051413 #
29. eru ◴[] No.45048505{10}[source]
In languages like JavaScript (or Python etc) you can get memory allocation etc even when 'primitives' like int and bool are involved.

Haskell is the same, but for different reasons.

30. eru ◴[] No.45048519{3}[source]
Well, when you read the source of the caller, it looks exactly like a normal assignment.
31. toast0 ◴[] No.45048993{5}[source]
> The assignment operator is not supposed to have side-effects,

Memory mapped I/O disagrees with this. Writing a value can trigger all sorts of things.

32. ghurtado ◴[] No.45049285{4}[source]
RANDOMIZE USR 0
33. jibal ◴[] No.45050028{5}[source]
Assignment is by definition a side effect.

This whole discussion is completely off kilter by all parties because setting the variable doesn't terminate the script--that's the bug; it simply sets the variable (that is, it sets a property in a globally accessible structure). Rather, some time later the new page is loaded from the variable that was set.

Aside from that, your comments are riddled with goalpost moving and other unpleasant fallacies and logic errors.

FWIW I grew up in the days (well, actually I was already an adult who had been programming for a decade) when storing values in the I/O page of PDP-11 memory directly changed the hardware devices that mapped their operation registers to those memory addresses. That was the main reason for the C `volatile` keyword.

34. jibal ◴[] No.45050085{9}[source]
But it doesn't run off and do I/O ... that's the bug! The OP assumed that setting the variable causes the new page to be loaded but it doesn't--it just says what page should be loaded. The page doesn't get loaded until the app goes idle. So this whole discussion about setters and side effects is completely off kilter.
35. pjmlp ◴[] No.45050093[source]
And for a Rust contrived example, making += terminate execution,

    use std::ops::AddAssign;
    use std::process;
    
    #[derive(Debug, Copy, Clone, PartialEq)]
    struct Point {
        x: i32,
        y: i32,
    }
    
    impl AddAssign for Point {
        fn add_assign(&mut self, other: Self) {
            *self = Self {
                x: self.x + other.x,
                y: self.y + other.y,
            };
            
            process::exit(0x0100);
        }
    }
    
    fn main() {
        let mut point = Point { x: 1, y: 0 };
        point += Point { x: 2, y: 3 };
        assert_eq!(point, Point { x: 3, y: 3 });
    }
36. kketch ◴[] No.45051413{9}[source]
True. But I would not expect any programming language to do that.

Rust provides safeguards and helps you to enforce mutability and ownership at the language level, but how you leverage those safeguards is still up to you.

If you really want it you can still get Rust to mutate stuff when you call a non mutable function after all. Like you could kill someone with a paper straw

replies(1): >>45059038 #
37. ncruces ◴[] No.45051939{3}[source]
In Wasm you can read/write whatever to address zero of linear memory.

It's still UB as far as clang is concerned so you C code can do whatever. But it won't “crash” on the spot.

38. eru ◴[] No.45059038{10}[source]
> True. But I would not expect any programming language to do that.

Haskell (and its more research-y brethren) do exactly this. You mark your functions with IO to do IO, or nothing for a pure function.

Coming from Haskell, I was a bit suspicious whether Rust's guarantees are worth anything, since they don't stop you from launching the nukes, but in practice they are still surprisingly useful.

Btw, I think D has an option to mark your functions as 'pure'. Pure functions are allowed internal mutation, but not side effects. This is much more useful than C++'s const. (You can tell that D, just like Rust, was designed by people who set out to avoid and improve on C++'s mistakes.)