News for hackers. Stuff that matters.
Re:What's new in C# for Godot 4.0 by Kiro on Sunday February 26, 2023 @10:13AM (#34946140)
How does it help with vector math?
Re:What's new in C# for Godot 4.0 by rmeby on Sunday February 26, 2023 @10:27AM (#34946241)
I would imagine operator overloading helps with vectors so you can do things like:

    public static Vector operator -(Vector a, Vector b)
    {
        Vector v = new Vector();
        v.X = a.X - b.X;
        v.Y = a.Y - b.Y;

        return v;
    }
So then you can just write:

    Vector result = myVector1 - myVector2;
Re:What's new in C# for Godot 4.0 by kevingadd on Sunday February 26, 2023 @11:11AM (#34946599)
Also if you wrote the JS equivalent of that, each vector would be 3-4 individually heap-allocated doubles in v8, because they aren't able to allocate floating-point values inline. There would also be a new heap allocation for the result vector itself when performing each operation. The overhead really adds up. :/

At one point Mozilla had a prototype implementation of strongly-typed value types in JavaScript, but it fell by the wayside once WebAssembly became a thing.

Re:What's new in C# for Godot 4.0 by ygra on Sunday February 26, 2023 @11:34AM (#34946802)
You can use an array to keep the doubles together. V8 has something like half a dozen different array types, depending on what is inside and how it's used.
Re:What's new in C# for Godot 4.0 by throwaway23532 on Sunday February 26, 2023 @06:12PM (#34950725)
Why not implement a method?

var result = myvector1.sub(myvector2)

Re:What's new in C# for Godot 4.0 by moonchrome on Sunday February 26, 2023 @03:55PM (#34949326)
Operator overloading makes simple math cleaner, but that's just a nice to have.

The real kicker is value type semantics. You have two options with reference types : you can either do math in-place or allocate on each operation. First option is a huge foot gun because the ownership is unclear it's super easy to share references to something and update something unintentionally. And it can be hard to track down. Second option is super taxing on GC. And the overhead of going through reference for simple 3 float tuple (and object type info, etc.) is large - but for large arrays you can sort of work around it by primitive arrays. But that's like hand rolling assembly and you're supposedly using a high level language.

C# with structs and operator overloading is an ideal high level language for gamedev - most of the time it's really ergonomic and high level, but it has the tools to get down to memory layout control without it looking like disassembled code.