Pointers

classic Classic list List threaded Threaded
17 messages Options
Reply | Threaded
Open this post in threaded view
|

Pointers

Sebastian Malton
Proposal:
Add a new variable-esk type called pointer which acts sort of like a reference to the data which it has been assigned to but modifies also the original reference when modified. 

Justification:
When working with objects in particular it is often useful for satiny reasons to use local variables (mostly const's) as a shorthand reference both for compactness and also so that you don't have to type out the entire path every type. This would allow for non - objects but also overwriting objects if wanted but still have this freedom and convenience. 

Method:
Add a new notation 

```
let:prt name = obj.field.name.fullname;
```

Here if `name` is updated or if `obj.field.name.fullname` is updated so is the other.

Sebastian Malton


_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
Reply | Threaded
Open this post in threaded view
|

Re: Pointers

Michael J. Ryan
Please no, mutable objects are bad enough imho.

On Mon, Mar 19, 2018, 12:47 Sebastian Malton <[hidden email]> wrote:
Proposal:
Add a new variable-esk type called pointer which acts sort of like a reference to the data which it has been assigned to but modifies also the original reference when modified. 

Justification:
When working with objects in particular it is often useful for satiny reasons to use local variables (mostly const's) as a shorthand reference both for compactness and also so that you don't have to type out the entire path every type. This would allow for non - objects but also overwriting objects if wanted but still have this freedom and convenience. 

Method:
Add a new notation 

```
let:prt name = obj.field.name.fullname;
```

Here if `name` is updated or if `obj.field.name.fullname` is updated so is the other.

Sebastian Malton

_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss

_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
Reply | Threaded
Open this post in threaded view
|

Re: Pointers

Isiah Meadows-2
And even if we *could* get pointers into JS, I'd *strongly* not want
it to be like what's proposed here. Instead, I'd prefer an object
encapsulating a reference to a variable, something like this (although
engines could avoid the ceremony of closures here):

```js
let foo = 1;

func(ref foo, bar)
// Equivalent to:
func({deref: () => foo, set: v => foo = v}, bar)

function func(ref foo, bar) {
    foo += 2
}

// Equivalent to:
function func(foo) {
    foo.set(foo.deref() + 2)
}
```

I've found myself more than once wanting a way to manipulate a
variable by reference, in a way that's a bit more ergonomic than just
creating a single-property object. It doesn't need to be much, but it
doesn't need to be much. As a concrete example, recursive string
joining could just pass a shared reference instead of an object with a
single property:

```js
function recursiveJoinLoop(ref str, array, sep) {
    if (Array.isArray(array)) {
        for (const item of array) recursiveJoinLoop(ref str, item, sep)
    } else {
        if (str == null) str = ""
        str += String(array)
    }
}

function recursiveJoin(array, sep) {
    let str
    for (const item of array) recursiveJoinLoop(ref str, array, sep)
    return str
}
```
-----

Isiah Meadows
[hidden email]

Looking for web consulting? Or a new website?
Send me an email and we can get started.
www.isiahmeadows.com


On Mon, Mar 19, 2018 at 5:48 PM, Michael J. Ryan <[hidden email]> wrote:

> Please no, mutable objects are bad enough imho.
>
> On Mon, Mar 19, 2018, 12:47 Sebastian Malton <[hidden email]> wrote:
>>
>> Proposal:
>> Add a new variable-esk type called pointer which acts sort of like a
>> reference to the data which it has been assigned to but modifies also the
>> original reference when modified.
>>
>> Justification:
>> When working with objects in particular it is often useful for satiny
>> reasons to use local variables (mostly const's) as a shorthand reference
>> both for compactness and also so that you don't have to type out the entire
>> path every type. This would allow for non - objects but also overwriting
>> objects if wanted but still have this freedom and convenience.
>>
>> Method:
>> Add a new notation
>>
>> ```
>> let:prt name = obj.field.name.fullname;
>> ```
>>
>> Here if `name` is updated or if `obj.field.name.fullname` is updated so is
>> the other.
>>
>> Sebastian Malton
>>
>> _______________________________________________
>> es-discuss mailing list
>> [hidden email]
>> https://mail.mozilla.org/listinfo/es-discuss
>
>
> _______________________________________________
> es-discuss mailing list
> [hidden email]
> https://mail.mozilla.org/listinfo/es-discuss
>
_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
Reply | Threaded
Open this post in threaded view
|

Re: Pointers

J Decker
Pointers are also useful for microoptimization of stepping through arrays... but it wouldn't help JS without a lot of work in the engines...

for( var x = 0; x < width*height; x++ ) {
    arr[x] = 3;  
}

-----

var ptr = arr;
for( var x = 0; x < width*height; x++ ) {
    ptr[0] = 3;  
    ptr++;
}

since a pointer plus a constant is always more efficient than a pointer plus a variable amount that usually also has to be multiplied.  

The second also serializes into SSE instructions when optimized so it can do multiple loop steps at a time.




On Mon, Mar 19, 2018 at 3:20 PM, Isiah Meadows <[hidden email]> wrote:
And even if we *could* get pointers into JS, I'd *strongly* not want
it to be like what's proposed here. Instead, I'd prefer an object
encapsulating a reference to a variable, something like this (although
engines could avoid the ceremony of closures here):

```js
let foo = 1;

func(ref foo, bar)
// Equivalent to:
func({deref: () => foo, set: v => foo = v}, bar)

function func(ref foo, bar) {
    foo += 2
}

// Equivalent to:
function func(foo) {
    foo.set(foo.deref() + 2)
}
```

I've found myself more than once wanting a way to manipulate a
variable by reference, in a way that's a bit more ergonomic than just
creating a single-property object. It doesn't need to be much, but it
doesn't need to be much. As a concrete example, recursive string
joining could just pass a shared reference instead of an object with a
single property:

```js
function recursiveJoinLoop(ref str, array, sep) {
    if (Array.isArray(array)) {
        for (const item of array) recursiveJoinLoop(ref str, item, sep)
    } else {
        if (str == null) str = ""
        str += String(array)
    }
}

function recursiveJoin(array, sep) {
    let str
    for (const item of array) recursiveJoinLoop(ref str, array, sep)
    return str
}
```
-----

Isiah Meadows
[hidden email]

Looking for web consulting? Or a new website?
Send me an email and we can get started.
www.isiahmeadows.com


On Mon, Mar 19, 2018 at 5:48 PM, Michael J. Ryan <[hidden email]> wrote:
> Please no, mutable objects are bad enough imho.
>
> On Mon, Mar 19, 2018, 12:47 Sebastian Malton <[hidden email]> wrote:
>>
>> Proposal:
>> Add a new variable-esk type called pointer which acts sort of like a
>> reference to the data which it has been assigned to but modifies also the
>> original reference when modified.
>>
>> Justification:
>> When working with objects in particular it is often useful for satiny
>> reasons to use local variables (mostly const's) as a shorthand reference
>> both for compactness and also so that you don't have to type out the entire
>> path every type. This would allow for non - objects but also overwriting
>> objects if wanted but still have this freedom and convenience.
>>
>> Method:
>> Add a new notation
>>
>> ```
>> let:prt name = obj.field.name.fullname;
>> ```
>>
>> Here if `name` is updated or if `obj.field.name.fullname` is updated so is
>> the other.
>>
>> Sebastian Malton
>>
>> _______________________________________________
>> es-discuss mailing list
>> [hidden email]
>> https://mail.mozilla.org/listinfo/es-discuss
>
>
> _______________________________________________
> es-discuss mailing list
> [hidden email]
> https://mail.mozilla.org/listinfo/es-discuss
>
_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss


_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
Reply | Threaded
Open this post in threaded view
|

RE: Pointers

Ron Buckton
In reply to this post by Isiah Meadows-2
> -----Original Message-----
> From: es-discuss <[hidden email]> On Behalf Of Isiah
> Meadows
> Sent: Monday, March 19, 2018 3:21 PM
> To: Michael J. Ryan <[hidden email]>
> Cc: es-discuss <[hidden email]>
> Subject: Re: Pointers
>
> And even if we *could* get pointers into JS, I'd *strongly* not want it to be
> like what's proposed here. Instead, I'd prefer an object encapsulating a
> reference to a variable, something like this (although engines could avoid the
> ceremony of closures here):
>
> ```js
> let foo = 1;
>
> func(ref foo, bar)
> // Equivalent to:
> func({deref: () => foo, set: v => foo = v}, bar)
>
> function func(ref foo, bar) {
>     foo += 2
> }
>
> // Equivalent to:
> function func(foo) {
>     foo.set(foo.deref() + 2)
> }
> ```

I put together a strawman for this last year at https://github.com/rbuckton/proposal-refs, but I haven't had much time to work on it.

Ron
_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
Reply | Threaded
Open this post in threaded view
|

Re: Pointers

Isiah Meadows-2
Yeah, I meant "references", not "pointers"... My bad.
-----

Isiah Meadows
[hidden email]

Looking for web consulting? Or a new website?
Send me an email and we can get started.
www.isiahmeadows.com


On Mon, Mar 19, 2018 at 7:57 PM, Ron Buckton <[hidden email]> wrote:

>> -----Original Message-----
>> From: es-discuss <[hidden email]> On Behalf Of Isiah
>> Meadows
>> Sent: Monday, March 19, 2018 3:21 PM
>> To: Michael J. Ryan <[hidden email]>
>> Cc: es-discuss <[hidden email]>
>> Subject: Re: Pointers
>>
>> And even if we *could* get pointers into JS, I'd *strongly* not want it to be
>> like what's proposed here. Instead, I'd prefer an object encapsulating a
>> reference to a variable, something like this (although engines could avoid the
>> ceremony of closures here):
>>
>> ```js
>> let foo = 1;
>>
>> func(ref foo, bar)
>> // Equivalent to:
>> func({deref: () => foo, set: v => foo = v}, bar)
>>
>> function func(ref foo, bar) {
>>     foo += 2
>> }
>>
>> // Equivalent to:
>> function func(foo) {
>>     foo.set(foo.deref() + 2)
>> }
>> ```
>
> I put together a strawman for this last year at https://github.com/rbuckton/proposal-refs, but I haven't had much time to work on it.
>
> Ron
_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
Reply | Threaded
Open this post in threaded view
|

Re: Pointers

Pier Bover
So the proposal is basically having references to primitives like strings, bools, etc?

On Mon, Mar 19, 2018, 6:59 PM Isiah Meadows <[hidden email]> wrote:
Yeah, I meant "references", not "pointers"... My bad.
-----

Isiah Meadows
[hidden email]

Looking for web consulting? Or a new website?
Send me an email and we can get started.
www.isiahmeadows.com


On Mon, Mar 19, 2018 at 7:57 PM, Ron Buckton <[hidden email]> wrote:
>> -----Original Message-----
>> From: es-discuss <[hidden email]> On Behalf Of Isiah
>> Meadows
>> Sent: Monday, March 19, 2018 3:21 PM
>> To: Michael J. Ryan <[hidden email]>
>> Cc: es-discuss <[hidden email]>
>> Subject: Re: Pointers
>>
>> And even if we *could* get pointers into JS, I'd *strongly* not want it to be
>> like what's proposed here. Instead, I'd prefer an object encapsulating a
>> reference to a variable, something like this (although engines could avoid the
>> ceremony of closures here):
>>
>> ```js
>> let foo = 1;
>>
>> func(ref foo, bar)
>> // Equivalent to:
>> func({deref: () => foo, set: v => foo = v}, bar)
>>
>> function func(ref foo, bar) {
>>     foo += 2
>> }
>>
>> // Equivalent to:
>> function func(foo) {
>>     foo.set(foo.deref() + 2)
>> }
>> ```
>
> I put together a strawman for this last year at https://github.com/rbuckton/proposal-refs, but I haven't had much time to work on it.
>
> Ron
_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss

_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
Reply | Threaded
Open this post in threaded view
|

Re: Pointers

Sebastian Malton
Yes that is basically what I want anyway 

Sebastian Malton

Sent: March 19, 2018 8:06 PM
Subject: Re: Pointers

So the proposal is basically having references to primitives like strings, bools, etc?

On Mon, Mar 19, 2018, 6:59 PM Isiah Meadows <[hidden email]> wrote:
Yeah, I meant "references", not "pointers"... My bad.
-----

Isiah Meadows
[hidden email]

Looking for web consulting? Or a new website?
Send me an email and we can get started.
www.isiahmeadows.com


On Mon, Mar 19, 2018 at 7:57 PM, Ron Buckton <[hidden email]> wrote:
>> -----Original Message-----
>> From: es-discuss <[hidden email]> On Behalf Of Isiah
>> Meadows
>> Sent: Monday, March 19, 2018 3:21 PM
>> To: Michael J. Ryan <[hidden email]>
>> Cc: es-discuss <[hidden email]>
>> Subject: Re: Pointers
>>
>> And even if we *could* get pointers into JS, I'd *strongly* not want it to be
>> like what's proposed here. Instead, I'd prefer an object encapsulating a
>> reference to a variable, something like this (although engines could avoid the
>> ceremony of closures here):
>>
>> ```js
>> let foo = 1;
>>
>> func(ref foo, bar)
>> // Equivalent to:
>> func({deref: () => foo, set: v => foo = v}, bar)
>>
>> function func(ref foo, bar) {
>>     foo += 2
>> }
>>
>> // Equivalent to:
>> function func(foo) {
>>     foo.set(foo.deref() + 2)
>> }
>> ```
>
> I put together a strawman for this last year at https://github.com/rbuckton/proposal-refs, but I haven't had much time to work on it.
>
> Ron
_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss

_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
Reply | Threaded
Open this post in threaded view
|

RE: Pointers

Ron Buckton
In reply to this post by Pier Bover
> -----Original Message-----
> From: Pier Bover <[hidden email]>
> Sent: Monday, March 19, 2018 5:06 PM
> To: Isiah Meadows <[hidden email]>
> Cc: Ron Buckton <[hidden email]>; es-discuss <es-
> [hidden email]>
> Subject: Re: Pointers
>
> So the proposal is basically having references to primitives like strings, bools,
> etc?

If you are talking about my proposal, then no. References wouldn't be limited to primitives, for example:

```js
function tryGet(map, key, ref value) {
  if (map.has(key)) {
    value = map.get(key);
    return true;
  }
  value = undefined;
  return false;
}

const map = new Map();
map.set("a", { x: 1 });
let value;
console.log(tryGet(map, "a", ref value), value); // true { x: 1 }
console.log(tryGet(map, "b", ref value), value); // false undefined
```

Ron
_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
Reply | Threaded
Open this post in threaded view
|

RE: Pointers

doodad-js Admin
In reply to this post by Ron Buckton
Ron: Very interesting, in particular, the function arguments by reference
aspect. Thanks.

Claude

-----Original Message-----
From: Ron Buckton <[hidden email]>
Sent: Monday, March 19, 2018 7:58 PM
To: Isiah Meadows <[hidden email]>; Michael J. Ryan
<[hidden email]>
Cc: es-discuss <[hidden email]>
Subject: RE: Pointers

> -----Original Message-----
> From: es-discuss <[hidden email]> On Behalf Of Isiah
> Meadows
> Sent: Monday, March 19, 2018 3:21 PM
> To: Michael J. Ryan <[hidden email]>
> Cc: es-discuss <[hidden email]>
> Subject: Re: Pointers
>
> And even if we *could* get pointers into JS, I'd *strongly* not want
> it to be like what's proposed here. Instead, I'd prefer an object
> encapsulating a reference to a variable, something like this (although
> engines could avoid the ceremony of closures here):
>
> ```js
> let foo = 1;
>
> func(ref foo, bar)
> // Equivalent to:
> func({deref: () => foo, set: v => foo = v}, bar)
>
> function func(ref foo, bar) {
>     foo += 2
> }
>
> // Equivalent to:
> function func(foo) {
>     foo.set(foo.deref() + 2)
> }
> ```

I put together a strawman for this last year at
https://github.com/rbuckton/proposal-refs, but I haven't had much time to
work on it.

Ron



---
This email has been checked for viruses by AVG.
http://www.avg.com

_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
Reply | Threaded
Open this post in threaded view
|

Re: Pointers

Isiah Meadows-2
In reply to this post by Ron Buckton
This is basically what I was proposing, ironically enough. There's a
few small differences, but the only one that substantially varied from
mine I filed an issue in your repo against. (I almost went for
`value`.)

BTW, yours looks a *lot* like OCaml's `ref 'a` type, which is just
sugar for `{mutable contents : 'a}`. The only difference is that OCaml
doesn't allow you to take a reference to a mutable property, while
yours does.
-----

Isiah Meadows
[hidden email]

Looking for web consulting? Or a new website?
Send me an email and we can get started.
www.isiahmeadows.com


On Mon, Mar 19, 2018 at 7:57 PM, Ron Buckton <[hidden email]> wrote:

>> -----Original Message-----
>> From: es-discuss <[hidden email]> On Behalf Of Isiah
>> Meadows
>> Sent: Monday, March 19, 2018 3:21 PM
>> To: Michael J. Ryan <[hidden email]>
>> Cc: es-discuss <[hidden email]>
>> Subject: Re: Pointers
>>
>> And even if we *could* get pointers into JS, I'd *strongly* not want it to be
>> like what's proposed here. Instead, I'd prefer an object encapsulating a
>> reference to a variable, something like this (although engines could avoid the
>> ceremony of closures here):
>>
>> ```js
>> let foo = 1;
>>
>> func(ref foo, bar)
>> // Equivalent to:
>> func({deref: () => foo, set: v => foo = v}, bar)
>>
>> function func(ref foo, bar) {
>>     foo += 2
>> }
>>
>> // Equivalent to:
>> function func(foo) {
>>     foo.set(foo.deref() + 2)
>> }
>> ```
>
> I put together a strawman for this last year at https://github.com/rbuckton/proposal-refs, but I haven't had much time to work on it.
>
> Ron
_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
Reply | Threaded
Open this post in threaded view
|

Re: Pointers

J Decker
Ahh parameters by reference yes have wished for that...

ref is C#-ism to me... which also maybe 'out' which really isn't different in JS.... in C# it enforces that the value is set in the function receiving an out 'must give value to parameter'.


On Mon, Mar 19, 2018 at 5:28 PM, Isiah Meadows <[hidden email]> wrote:
This is basically what I was proposing, ironically enough. There's a
few small differences, but the only one that substantially varied from
mine I filed an issue in your repo against. (I almost went for
`value`.)

BTW, yours looks a *lot* like OCaml's `ref 'a` type, which is just
sugar for `{mutable contents : 'a}`. The only difference is that OCaml
doesn't allow you to take a reference to a mutable property, while
yours does.
-----

Isiah Meadows
[hidden email]

Looking for web consulting? Or a new website?
Send me an email and we can get started.
www.isiahmeadows.com


On Mon, Mar 19, 2018 at 7:57 PM, Ron Buckton <[hidden email]> wrote:
>> -----Original Message-----
>> From: es-discuss <[hidden email]> On Behalf Of Isiah
>> Meadows
>> Sent: Monday, March 19, 2018 3:21 PM
>> To: Michael J. Ryan <[hidden email]>
>> Cc: es-discuss <[hidden email]>
>> Subject: Re: Pointers
>>
>> And even if we *could* get pointers into JS, I'd *strongly* not want it to be
>> like what's proposed here. Instead, I'd prefer an object encapsulating a
>> reference to a variable, something like this (although engines could avoid the
>> ceremony of closures here):
>>
>> ```js
>> let foo = 1;
>>
>> func(ref foo, bar)
>> // Equivalent to:
>> func({deref: () => foo, set: v => foo = v}, bar)
>>
>> function func(ref foo, bar) {
>>     foo += 2
>> }
>>
>> // Equivalent to:
>> function func(foo) {
>>     foo.set(foo.deref() + 2)
>> }
>> ```
>
> I put together a strawman for this last year at https://github.com/rbuckton/proposal-refs, but I haven't had much time to work on it.
>
> Ron
_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss


_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
Reply | Threaded
Open this post in threaded view
|

Re: Pointers

Isiah Meadows-2
C# isn't the only OO language to have references. PHP has them too,
although the awkwardness isn't the references themselves as much as
how the language integrates with them. OCaml also has reified
references as separate from their mutable record properties.

Note that C#'s "out" parameters aren't the same as what's being
proposed here. Ron's proposal shares more in common with Rust's `foo:
&mut Foo` parameters and C/C++'s `&foo` (address-of) operator than
C#'s `out` parameters, since the proposal doesn't restrict their use
to just parameters, and it doesn't just treat them as alternate return
targets. C# has those just for C interop and little else, while Rust
and OCaml have them for general use. If we're going to have
general-use references, let's not mimic C#'s very awkward mess when we
do it.

(JS is a much lower level language than it looks. It abstracts less
than most other common dynamic languages.)

-----

Isiah Meadows
[hidden email]

Looking for web consulting? Or a new website?
Send me an email and we can get started.
www.isiahmeadows.com


On Mon, Mar 19, 2018 at 8:37 PM, J Decker <[hidden email]> wrote:

> Ahh parameters by reference yes have wished for that...
>
> ref is C#-ism to me... which also maybe 'out' which really isn't different
> in JS.... in C# it enforces that the value is set in the function receiving
> an out 'must give value to parameter'.
>
>
> On Mon, Mar 19, 2018 at 5:28 PM, Isiah Meadows <[hidden email]>
> wrote:
>>
>> This is basically what I was proposing, ironically enough. There's a
>> few small differences, but the only one that substantially varied from
>> mine I filed an issue in your repo against. (I almost went for
>> `value`.)
>>
>> BTW, yours looks a *lot* like OCaml's `ref 'a` type, which is just
>> sugar for `{mutable contents : 'a}`. The only difference is that OCaml
>> doesn't allow you to take a reference to a mutable property, while
>> yours does.
>> -----
>>
>> Isiah Meadows
>> [hidden email]
>>
>> Looking for web consulting? Or a new website?
>> Send me an email and we can get started.
>> www.isiahmeadows.com
>>
>>
>> On Mon, Mar 19, 2018 at 7:57 PM, Ron Buckton <[hidden email]>
>> wrote:
>> >> -----Original Message-----
>> >> From: es-discuss <[hidden email]> On Behalf Of Isiah
>> >> Meadows
>> >> Sent: Monday, March 19, 2018 3:21 PM
>> >> To: Michael J. Ryan <[hidden email]>
>> >> Cc: es-discuss <[hidden email]>
>> >> Subject: Re: Pointers
>> >>
>> >> And even if we *could* get pointers into JS, I'd *strongly* not want it
>> >> to be
>> >> like what's proposed here. Instead, I'd prefer an object encapsulating
>> >> a
>> >> reference to a variable, something like this (although engines could
>> >> avoid the
>> >> ceremony of closures here):
>> >>
>> >> ```js
>> >> let foo = 1;
>> >>
>> >> func(ref foo, bar)
>> >> // Equivalent to:
>> >> func({deref: () => foo, set: v => foo = v}, bar)
>> >>
>> >> function func(ref foo, bar) {
>> >>     foo += 2
>> >> }
>> >>
>> >> // Equivalent to:
>> >> function func(foo) {
>> >>     foo.set(foo.deref() + 2)
>> >> }
>> >> ```
>> >
>> > I put together a strawman for this last year at
>> > https://github.com/rbuckton/proposal-refs, but I haven't had much time to
>> > work on it.
>> >
>> > Ron
>> _______________________________________________
>> es-discuss mailing list
>> [hidden email]
>> https://mail.mozilla.org/listinfo/es-discuss
>
>
_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
Reply | Threaded
Open this post in threaded view
|

Re: Pointers

J Decker


On Mon, Mar 19, 2018 at 6:01 PM, Isiah Meadows <[hidden email]> wrote:
C# isn't the only OO language to have references. PHP has them too,
although the awkwardness isn't the references themselves as much as
how the language integrates with them. OCaml also has reified
references as separate from their mutable record properties.

Note that C#'s "out" parameters aren't the same as what's being
proposed here. Ron's proposal shares more in common with Rust's `foo:
&mut Foo` parameters and C/C++'s `&foo` (address-of) operator than
C#'s `out` parameters, since the proposal doesn't restrict their use
to just parameters, and it doesn't just treat them as alternate return
targets. C# has those just for C interop and little else, while Rust
and OCaml have them for general use. If we're going to have
general-use references, let's not mimic C#'s very awkward mess when we
do it.

(JS is a much lower level language than it looks. It abstracts less
than most other common dynamic languages.)


I didn't mean to stress 'out'.  'ref' was really the only thing I meant for a take-away.

other than in parameters, I don't see a use... 
I went back to the top and re-read with these new colored glasses...

var:prt is terrible; that would be a printer...or maybe part  :)  var:ptr; and since it's not a pointer, maybe ref instead?


what if the property is a  setter/getter reference when getting a set/get property.

var x = { 
  name : 'foo',
  get Name() { return name } 
  set Name() { this.name = name };
}

var:ref getterRef = x.Name  // retain both set and get?

var:ref xName = x.name;

(then changing either xName or x.name (or x.Name) to a new value

 
-----

Isiah Meadows
[hidden email]

Looking for web consulting? Or a new website?
Send me an email and we can get started.
www.isiahmeadows.com


On Mon, Mar 19, 2018 at 8:37 PM, J Decker <[hidden email]> wrote:
> Ahh parameters by reference yes have wished for that...
>
> ref is C#-ism to me... which also maybe 'out' which really isn't different
> in JS.... in C# it enforces that the value is set in the function receiving
> an out 'must give value to parameter'.
>
>
> On Mon, Mar 19, 2018 at 5:28 PM, Isiah Meadows <[hidden email]>
> wrote:
>>
>> This is basically what I was proposing, ironically enough. There's a
>> few small differences, but the only one that substantially varied from
>> mine I filed an issue in your repo against. (I almost went for
>> `value`.)
>>
>> BTW, yours looks a *lot* like OCaml's `ref 'a` type, which is just
>> sugar for `{mutable contents : 'a}`. The only difference is that OCaml
>> doesn't allow you to take a reference to a mutable property, while
>> yours does.
>> -----
>>
>> Isiah Meadows
>> [hidden email]
>>
>> Looking for web consulting? Or a new website?
>> Send me an email and we can get started.
>> www.isiahmeadows.com
>>
>>
>> On Mon, Mar 19, 2018 at 7:57 PM, Ron Buckton <[hidden email]>
>> wrote:
>> >> -----Original Message-----
>> >> From: es-discuss <[hidden email]> On Behalf Of Isiah
>> >> Meadows
>> >> Sent: Monday, March 19, 2018 3:21 PM
>> >> To: Michael J. Ryan <[hidden email]>
>> >> Cc: es-discuss <[hidden email]>
>> >> Subject: Re: Pointers
>> >>
>> >> And even if we *could* get pointers into JS, I'd *strongly* not want it
>> >> to be
>> >> like what's proposed here. Instead, I'd prefer an object encapsulating
>> >> a
>> >> reference to a variable, something like this (although engines could
>> >> avoid the
>> >> ceremony of closures here):
>> >>
>> >> ```js
>> >> let foo = 1;
>> >>
>> >> func(ref foo, bar)
>> >> // Equivalent to:
>> >> func({deref: () => foo, set: v => foo = v}, bar)
>> >>
>> >> function func(ref foo, bar) {
>> >>     foo += 2
>> >> }
>> >>
>> >> // Equivalent to:
>> >> function func(foo) {
>> >>     foo.set(foo.deref() + 2)
>> >> }
>> >> ```
>> >
>> > I put together a strawman for this last year at
>> > https://github.com/rbuckton/proposal-refs, but I haven't had much time to
>> > work on it.
>> >
>> > Ron
>> _______________________________________________
>> es-discuss mailing list
>> [hidden email]
>> https://mail.mozilla.org/listinfo/es-discuss
>
>


_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
Reply | Threaded
Open this post in threaded view
|

Re: Pointers

J Decker

On Mon, Mar 19, 2018 at 6:17 PM, J Decker <[hidden email]> wrote:


On Mon, Mar 19, 2018 at 6:01 PM, Isiah Meadows <[hidden email]> wrote:
C# isn't the only OO language to have references. PHP has them too,
although the awkwardness isn't the references themselves as much as
how the language integrates with them. OCaml also has reified
references as separate from their mutable record properties.

Note that C#'s "out" parameters aren't the same as what's being
proposed here. Ron's proposal shares more in common with Rust's `foo:
&mut Foo` parameters and C/C++'s `&foo` (address-of) operator than
C#'s `out` parameters, since the proposal doesn't restrict their use
to just parameters, and it doesn't just treat them as alternate return
targets. C# has those just for C interop and little else, while Rust
and OCaml have them for general use. If we're going to have
general-use references, let's not mimic C#'s very awkward mess when we
do it.

(JS is a much lower level language than it looks. It abstracts less
than most other common dynamic languages.)


I didn't mean to stress 'out'.  'ref' was really the only thing I meant for a take-away.

other than in parameters, I don't see a use... 
I went back to the top and re-read with these new colored glasses...

var:prt is terrible; that would be a printer...or maybe part  :)  var:ptr; and since it's not a pointer, maybe ref instead?


what if the property is a  setter/getter reference when getting a set/get property.

var x = { 
  name : 'foo',
  get Name() { return name } 
  set Name() { this.name = name };
}

var:ref getterRef = x.Name  // retain both set and get?

var:ref xName = x.name;

(then changing either xName or x.name (or x.Name) to a new value


and I meant to say you could keep references

var xNameRef = { obj : x, field : 'name' }

xNameRef.obj[xNameRef.field]


as a generic way of referencing anything. ...
 
 
-----

Isiah Meadows
[hidden email]

Looking for web consulting? Or a new website?
Send me an email and we can get started.
www.isiahmeadows.com


On Mon, Mar 19, 2018 at 8:37 PM, J Decker <[hidden email]> wrote:
> Ahh parameters by reference yes have wished for that...
>
> ref is C#-ism to me... which also maybe 'out' which really isn't different
> in JS.... in C# it enforces that the value is set in the function receiving
> an out 'must give value to parameter'.
>
>
> On Mon, Mar 19, 2018 at 5:28 PM, Isiah Meadows <[hidden email]>
> wrote:
>>
>> This is basically what I was proposing, ironically enough. There's a
>> few small differences, but the only one that substantially varied from
>> mine I filed an issue in your repo against. (I almost went for
>> `value`.)
>>
>> BTW, yours looks a *lot* like OCaml's `ref 'a` type, which is just
>> sugar for `{mutable contents : 'a}`. The only difference is that OCaml
>> doesn't allow you to take a reference to a mutable property, while
>> yours does.
>> -----
>>
>> Isiah Meadows
>> [hidden email]
>>
>> Looking for web consulting? Or a new website?
>> Send me an email and we can get started.
>> www.isiahmeadows.com
>>
>>
>> On Mon, Mar 19, 2018 at 7:57 PM, Ron Buckton <[hidden email]>
>> wrote:
>> >> -----Original Message-----
>> >> From: es-discuss <[hidden email]> On Behalf Of Isiah
>> >> Meadows
>> >> Sent: Monday, March 19, 2018 3:21 PM
>> >> To: Michael J. Ryan <[hidden email]>
>> >> Cc: es-discuss <[hidden email]>
>> >> Subject: Re: Pointers
>> >>
>> >> And even if we *could* get pointers into JS, I'd *strongly* not want it
>> >> to be
>> >> like what's proposed here. Instead, I'd prefer an object encapsulating
>> >> a
>> >> reference to a variable, something like this (although engines could
>> >> avoid the
>> >> ceremony of closures here):
>> >>
>> >> ```js
>> >> let foo = 1;
>> >>
>> >> func(ref foo, bar)
>> >> // Equivalent to:
>> >> func({deref: () => foo, set: v => foo = v}, bar)
>> >>
>> >> function func(ref foo, bar) {
>> >>     foo += 2
>> >> }
>> >>
>> >> // Equivalent to:
>> >> function func(foo) {
>> >>     foo.set(foo.deref() + 2)
>> >> }
>> >> ```
>> >
>> > I put together a strawman for this last year at
>> > https://github.com/rbuckton/proposal-refs, but I haven't had much time to
>> > work on it.
>> >
>> > Ron
>> _______________________________________________
>> es-discuss mailing list
>> [hidden email]
>> https://mail.mozilla.org/listinfo/es-discuss
>
>



_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
Reply | Threaded
Open this post in threaded view
|

Re: Pointers

Mike Samuel
In reply to this post by Sebastian Malton
There is a spec reference type.  It was initially there for host objects, and IIRC, most were happy to see it go.




8.7 The Reference Type
The internal Reference type is not a language data type.

It is defined by this specification purely for expository purposes. An implementation of ECMAScript must behave as if it produced and operated upon references in the manner described here. However, a value of type Reference is used only as an intermediate result of expression evaluation and cannot be stored as the value of a variable or property.

The Reference type is used to explain the behaviour of such operators as delete, typeof, and the assignment operators. For example, the left-hand operand of an assignment is expected to produce a reference. The behaviour of assignment could, instead, be explained entirely in terms of a case analysis on the syntactic form of the left-hand operand of an assignment operator, but for one difficulty: function calls are permitted to return references. This possibility is admitted purely for the sake of host objects. No builtin ECMAScript function defined by this specification returns a reference and there is no provision for a user-defined function to return a reference. (Another reason not to use a syntactic case analysis is that it would be lengthy and awkward, affecting many parts of the specification.)

Another use of the Reference type is to explain the determination of the this value for a function call. A Reference is a reference to a property of an object.

A Reference consists of two components, the base object and the property name.

The following abstract operations are used in this specification to access the components of references:
• GetBase(V). Returns the base object component of the reference V.
• GetPropertyName(V). Returns the property name component of the reference V.

The following abstract operations are used in this specification to operate on references:



http://www.ecma-international.org/ecma-262/#sec-reference-specification-type is the much reduced equivalent in the current draft.
6.2.4The Reference Specification Type


Trying to expose the reference specification type as an actual user manipulable type would have a lot of consequences, and would not
actually address the local variable use case.


If your use case can be satisfied by objects like some syntactic sugar like
    { get content() { return x }, set content(v) { return x = v } }
then you might find it easier asking for that instead of a new type.




On Mon, Mar 19, 2018 at 3:47 PM, Sebastian Malton <[hidden email]> wrote:
Proposal:
Add a new variable-esk type called pointer which acts sort of like a reference to the data which it has been assigned to but modifies also the original reference when modified. 

Justification:
When working with objects in particular it is often useful for satiny reasons to use local variables (mostly const's) as a shorthand reference both for compactness and also so that you don't have to type out the entire path every type. This would allow for non - objects but also overwriting objects if wanted but still have this freedom and convenience. 

Method:
Add a new notation 

```
let:prt name = obj.field.name.fullname;
```

Here if `name` is updated or if `obj.field.name.fullname` is updated so is the other.

Sebastian Malton


_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss



_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
Reply | Threaded
Open this post in threaded view
|

Re: Pointers

Andrea Giammarchi-2
In reply to this post by Sebastian Malton
It sounds like you'd like to have the `with` statement back.

On Mon, Mar 19, 2018 at 8:47 PM, Sebastian Malton <[hidden email]> wrote:
a reference to the data which it has been assigned to but modifies also the original reference when modified. 

```js
const obj = {field: {name: {fullname: 'before'}}};

  console.log(fullname);
  fullname = 'after';
}

console.log(obj.field.name.fullname);
```

Combined `with(aProxy)` that pattern was able to do so much magic !!! ... although I'm not sure it's a good idea to re-introduce it through self-contained references.

Regards





_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss