sirisian / ecmascript-types

ECMAScript Optional Static Typing Proposal http://sirisian.github.io/ecmascript-types/
453 stars 4 forks source link

Add reference syntax to the proposal to work with all value types including class ones #72

Closed sirisian closed 3 weeks ago

sirisian commented 2 years ago

Since performance is a core part of adding native types it's probably good to ensure that any reference proposal syntax is compatible with value type classes and such.

Related to #71 (which is still a WIP) since one might reference array elements.

https://es.discourse.group/t/passing-by-reference/865/12 https://es.discourse.group/t/draft-for-a-proposal-of-an-explicit-reference-syntax/1475

I quite like using & token over a ref keyword, but that can be figured out later.

class A {
  a:uint32
  b:uint32
}
const a:[10]<A>;
const &b = a[0];
b.a = 10;

function F(&c:A) { // Takes a reference to A
  c.a = 10;
}
F(&a[1]); // Explicitly passes a reference
function F(&a) {
  a++;
}
let a = 10;
F(&a);

Stack issues detected by the scope?

function Foo():A {
  const a:[10]<A>;
  //return &a[0]; // Invalid, a is on the stack.
  return a[0];
}

Also WIP, but I kind of want to "fix" Map if possible with this. The ability for function to return references. hmm this seems difficult:

https://es.discourse.group/t/map-weakmap-etc-prototype-getref-key/884

The FAQ for emplace has a section about this as well with returning multiple references and order of operations and such.

const a:[10]<int32>;
function F(a):int32 { // Is the return type the same or like a reference type?
  return &a[0];
}
a[0] = 5;
F(a)++;
a[0]; // 6
let &b = F(a);
b = 10;
a[0]; // 10

One could imagine syntax to get a map item that returns a reference. Not sure what would happen if it didn't exist. Undefined?

Can one reassign a reference?

const a:[10]<int32>;
let &b = a[0];
&b = a[1];
sirisian commented 2 years ago

Added initial changes here:

https://github.com/sirisian/ecmascript-types/blob/master/README.md#value-type-references

sirisian commented 3 weeks ago

I've deferred to the ref proposal which seems sound. Changed to ref keyword rather than complicate things with using & which is used in intersection types probably.