sweet-js / sweet-core

Sweeten your JavaScript.
https://www.sweetjs.org
BSD 2-Clause "Simplified" License
4.58k stars 208 forks source link

Array accessor custom operator #714

Open ghost opened 7 years ago

ghost commented 7 years ago

Hello. I also have idea for custom operators with arrays.

operator {@} accessor 19 = (object, index) => {
  return #`[object, index]`; // for feed some functions with spreads, for example - Atomics
}

Where symbol @ are divisor between { and }.

gabejohnson commented 7 years ago

For your proposed syntax, you'll have to wait for #687 to be resolved.

I'm not sure what accessor is supposed to designate in your operator declaration.

ghost commented 7 years ago

I mean argument that means key/index.

gabejohnson commented 7 years ago

Can you provide example usage?

ghost commented 7 years ago

Really good for SIMD, Typed Array operations, Atomics.

let arrayPtr = typedarray{0}; // getting [typedarray, 0]
// also would be operators with pointers i.e. right pair value 
let value = *arrayPtr; // arrayPtr[0][arrayPtr[1]] as getter
*arrayPtr = anotherValue; // arrayPtr[0][arrayPtr[1]] as setter
Atomics.add(...arrayPtr, 1); // using ptr as arguments of Atomics (workers)
gabejohnson commented 7 years ago

There's currently no way to use operator or syntax to modify the meaning of delimiters ([],{},()). You'd have to extend the reader.

ghost commented 7 years ago

I can do array $ptr 0 but this is not very nice syntax. I want like this array |> 0.