es-explicit-this icon indicating copy to clipboard operation
es-explicit-this copied to clipboard

Another alternate syntax :D

Open xooxdoo opened this issue 8 years ago • 2 comments

function ::zip (array, otherArray) {
  return array.map( (a, i) => [a, otherArray[i]] )
}

function ::flatten (subject) {
  return subject.reduce( (a,b) => a.concat(b) )
}

[10,20]
  ::zip( [1,2] )
  ::flatten()
//=> [10, 1, 20, 2]

// Flow/TypeScript/SoundScript
function ::zip<T>(array: Array<T>, otherArray: Array<T>): Array<T> {
  return array.map( (a, i) => [a, otherArray[i]] )
}

function ::flatten<T>(subject: Array<T>): T {
  return subject.reduce( (a,b) => a.concat(b) );
}

As you can see this solves the too many colones you mentioned in #1. And keep the aspects of what i was talking about transparency between the two specs :D.

Also i couldn't grasp the fact that in the first syntax ...

function method (this elem, e) {
//               ^-- `this` is an argument; I simply renamed it therefor I can use it
  console.log("Elem:", elem) // OK
  console.log("this:", this) // <-- Throws an error!
//                       ^-- ISSUE: `this` is allowed in the signature and not the body

It seems logic to me that if you are not going to let me use this keyword inside the function, you should ask me to use it in the argument either :D.

Wish the best for you and your suggestion, but please take my syntax in consideration.

And as I suggested in #1 consider merging it with the other spec if that is possible.

xooxdoo avatar Apr 06 '16 22:04 xooxdoo

Here is another alternative from TypeScript community (https://github.com/Microsoft/TypeScript/issues/6018#issuecomment-164757557)

function filter<T> Iterable<T>::(callback: (value: T) => boolean): Iterable<T> { ... }

Eliminate the type annotations and add thisArg:

function filter thisArg::(callback) { ... }
function zip array::(otherArray) {
  return array.map( (a, i) => [a, otherArray[i]] )
}

function flatten subject::() {
  return subject.reduce( (a,b) => a.concat(b) )
}

I don't much like this syntax, just FYI.

hax avatar Jun 15 '16 16:06 hax

Also I used C# more the past few months and this is very helpful to add functionalities to third party objects without extending them, a really like it now.

they should really add this to JS

xooxdoo avatar Jun 16 '16 22:06 xooxdoo