3 ms·
I spent a lot of this weekend learning Zig, and this was the most surprising this for me. foo, bar, and baz are all called the same way (as thing.foo() or thing
by MatthiasPortzel 4y ago
I spent a lot of this weekend learning Zig, and this was the most surprising this for me. foo, bar, and baz are all called the same way (as thing.foo() or thing.baz()), but depending on the type signature, the compiler can figure out whether you want a mutable or immutable reference to the object.
- masklinn 4y agoIt’s pretty common for languages in the field e.g. C++ (const versus non-const methods), Rust (value, unique reference, shared reference, deref), and even Go (value v pointer, delegation). The “method syntax” is pretty much just a convenience, so why not also handle that after all?
- WalterBright 4y agoThis is common behavior for languages. D adds another wrinkle to it - a reference can be mutable, const, or immutable. mutable: can modify it const: cannot modify it through the reference, but other mutable references can still modify it immutable: nobody can modify it The 'immutable' is great for writing threaded code, as no synchronization is necessary.