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

You could overload operator=() in C++ with a call to exit(), which fulfills "variable assignment that halts the program".


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 });
    }


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).


idk if I'd consider overloading the assignment operator to call a function, then using it, actually an assignment in truth.


Well, when you read the source of the caller, it looks exactly like a normal assignment.




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

Search: