There, creating and destructuring a hash use matching syntax {}
I also don’t like single character names for length, iteration, and don’t understand the type system. :I is an integer, but :I-R? I can’t figure out what that means from this example.
EGreg · 51m 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.
CureYooz · 3h ago
How about an example that is actually readable and easy to follow?
EGreg · 34m ago
Here, I will write one by hand right now:
d IntegerMath : SomeInterface, SomeOtherInterface
someProperty:I
someOtherProperty:I-M
referenceProperty:IntegerMath+R+N // nullable
recursiveProperty:IntegerMath+N // error! needs +R
f stuff(myObj: IntegerMath+M+R, newValue: I+M)
newValue = 4 // modifies caller's reference
myObj.someProperty = newValue // error, need newValue.c()
myObj.someOtherProperty = 4 // error, immutable property
IntegerMath.max = f(first:I, second:I):I
r => first > second ? first : second
IntegerMath.min = f(first:I, second:I):I => first < second ? first : second
// let's use the class
im = IntegerMath.n({someOtherProperty: 4}) // infers im:IntegerMath
im.someProperty = 3
im.stuff(im, someInteger) // modifies both
result = Math.sort([1,2,3], IntegerMath.max) // infers result:[I]
another = Math.sort(result, f(first:I, second:I):I => first > second ? first : second)
// conditional code
[1,2,3].x( f(k)
k > 2
? console.log(k)
: (
result = k
console.log(k-2)
)
// this is a closure but doesn't assign
// anything to +R variables, so it's not +R itself
// in Swift language, it's not "escaping"
I also don’t like single character names for length, iteration, and don’t understand the type system. :I is an integer, but :I-R? I can’t figure out what that means from this example.
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.