3 ms·
Yeah, I shouldn't have used ChatGPT to create the example I posted. It still messes up (since it wasn't trained on the language at all, of course). I will fix u
by EGreg 1y ago
Yeah, I shouldn't have used ChatGPT to create the example I posted. It still messes up (since it wasn't trained on the language at all, of course). I will fix up the examples, but it should be very obvious and clear, never "weird"... see the spec in the link.
Basically, -R is the default anyway everywhere. +R means "remote" or "retained", i.e. the variable doesn't live on the stack. By default, parameters passed to functions are -R and can only be assigned to other -R variables. If you try to assign a -R to a +R variable or its property, you have to do .c() to mark it to be potentially copied to the heap. This is to explicitly prevent use-after-free errors or dangling pointers.
Basically, +M on a parameter to a function means it can be modified, it becomes passed by reference. +M with or without +R is just modifying the reference in-place, but without +R you can't assign it to any +R variable or its property without calling .c() on it. The c() marks it as copy-on-write which makes a copy if its destructor runs and it still has references to it. If it only has one reference to it, then obviously we avoid making a copy on write.
In functions, -M is the default for parameters, but in classes, +M is the default for properties (mutable) and you have to mark it -M explicitly (like const keyword).
Also +N means something can be nullable, which is another set of explicit optimizations (dereferencing a variable without a guard gives a compile-time error if it's nullable).
A class is basically a struct by default, i.e. all properties are local (-R is implicit) but if you mark some properties +R then they become pointers to other areas in memory. You might want to do this if e.g. multiple objects have properties that reference the same objects.
Also, unlike Rust which has only one writer, we have the idea of transparent proxy objects, which can use various MVCC strategies (e.g. caching an old value) to avoid concurrent race conditions. In this way it is even designed to support decentralized code, across multiple machines.
The language is designed to catch a wide set of mistakes (typing = when you meant ==, etc.) and let you inspect retaining graphs for memory leaks and much more. In short, it is supposed to be easier to grok than Rust, and in some cases even safer, without needing borrow checkers etc.
- Someone 1y ago> +R means "remote" or "retained", i.e. the variable doesn't live on the stack I think that’s a bad name. Both “remote” (https://en.wikipedia.org/wiki/Remote_procedure_call https://en.wikipedia.org/wiki/Remote_procedure_call) and “retained” (https://en.wikipedia.org/wiki/Retained_mode https://en.wikipedia.org/wiki/Retained_mode, https://developer.apple.com/documentation/objectivec/nsobjectprotocol/1571946-retain https://developer.apple.com/documentation/objectivec/nsobjec...) are existing terms in programming with wildly different meaning. What’s wrong with “reference” (https://en.wikipedia.org/wiki/Reference_(computer_science) https://en.wikipedia.org/wiki/Reference_(computer_science)) ?
- EGreg 1y agoThe R can stand for anything It’s just a letter after all But you should be able to retain/release this variable, and it is not on the stack I was considering +G for GPU SIMD vectors, but didnt know if I need to