Proposal to add symbol: "hasInstanceStrict"

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

Proposal to add symbol: "hasInstanceStrict"

Александр Ефремов
It’s attempt to provide intermediate layer for implementation of runtime type checking.

```
class PrimitiveNumber {
        static [Symbol.hasInstanceStrict](x) {
                if (typeof x !== ’number’) {
                        throw new TypeError(‘Invalid type’);
                }
        }
}

function sum(a: PrimitiveNumber, b: PrimitiveNumber) {
        return a + b;
}

const c: PrimitiveNumber = sum(1, 2);
```

I.e. when appears assignment of variable (const) if then follows `: <class>` then JS runtime must to call method `static [Symbol.hasInstanceStrict](x)` and transfer to there assignable value.

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

Re: Proposal to add symbol: "hasInstanceStrict"

kdex
Can you give some reasons why this, as opposed to static type checking, is
worth pursuing at all?

On Sunday, February 18, 2018 5:30:42 PM CET Aleksander Efremov wrote:

> It’s attempt to provide intermediate layer for implementation of runtime
> type checking.
>
> ```
> class PrimitiveNumber {
> static [Symbol.hasInstanceStrict](x) {
> if (typeof x !== ’number’) {
> throw new TypeError(‘Invalid type’);
> }
> }
> }
>
> function sum(a: PrimitiveNumber, b: PrimitiveNumber) {
> return a + b;
> }
>
> const c: PrimitiveNumber = sum(1, 2);
> ```
>
> I.e. when appears assignment of variable (const) if then follows `: <class>`
> then JS runtime must to call method `static [Symbol.hasInstanceStrict](x)`
> and transfer to there assignable value.
>
> _______________________________________________
> 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

signature.asc (849 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: Proposal to add symbol: "hasInstanceStrict"

Michał Wadas
In reply to this post by Александр Ефремов
Runtime type checking isn't widely deployed for many reasons. Programmable runtime type checking would be even worse because engine can't just throw away function call (only extreme minority of JS function can be proven to be pure). 

On 18 Feb 2018 5:30 pm, "Aleksander Efremov" <[hidden email]> wrote:
It’s attempt to provide intermediate layer for implementation of runtime type checking.

```
class PrimitiveNumber {
        static [Symbol.hasInstanceStrict](x) {
                if (typeof x !== ’number’) {
                        throw new TypeError(‘Invalid type’);
                }
        }
}

function sum(a: PrimitiveNumber, b: PrimitiveNumber) {
        return a + b;
}

const c: PrimitiveNumber = sum(1, 2);
```

I.e. when appears assignment of variable (const) if then follows `: <class>` then JS runtime must to call method `static [Symbol.hasInstanceStrict](x)` and transfer to there assignable value.

_______________________________________________
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: Proposal to add symbol: "hasInstanceStrict"

Александр Ефремов
If data were received from database or isn't trusted sources and you want want be sure that they is correct then it's impossible to do with static type checking.
And also therefore I don't offer to call function before the real case, I wrote that that check should be executed in runtime, it isn't compiling stage similar to compiling language.

18 февр. 2018 г. 20:56 пользователь "Michał Wadas" <[hidden email]> написал:
Runtime type checking isn't widely deployed for many reasons. Programmable runtime type checking would be even worse because engine can't just throw away function call (only extreme minority of JS function can be proven to be pure). 

On 18 Feb 2018 5:30 pm, "Aleksander Efremov" <[hidden email]> wrote:
It’s attempt to provide intermediate layer for implementation of runtime type checking.

```
class PrimitiveNumber {
        static [Symbol.hasInstanceStrict](x) {
                if (typeof x !== ’number’) {
                        throw new TypeError(‘Invalid type’);
                }
        }
}

function sum(a: PrimitiveNumber, b: PrimitiveNumber) {
        return a + b;
}

const c: PrimitiveNumber = sum(1, 2);
```

I.e. when appears assignment of variable (const) if then follows `: <class>` then JS runtime must to call method `static [Symbol.hasInstanceStrict](x)` and transfer to there assignable value.

_______________________________________________
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: Proposal to add symbol: "hasInstanceStrict"

Mike Samuel
In reply to this post by Александр Ефремов
Must hasInstanceStrict's parameter remain untyped for a guard to complete or is there a top type with an unguarded hasInstanceStrict that these checks bottom out on?

"has" to my ear sounds like it should return a boolean.  Maybe "require."



On Feb 18, 2018 11:30 AM, "Aleksander Efremov" <[hidden email]> wrote:
It’s attempt to provide intermediate layer for implementation of runtime type checking.

```
class PrimitiveNumber {
        static [Symbol.hasInstanceStrict](x) {
                if (typeof x !== ’number’) {
                        throw new TypeError(‘Invalid type’);
                }
        }
}

function sum(a: PrimitiveNumber, b: PrimitiveNumber) {
        return a + b;
}

const c: PrimitiveNumber = sum(1, 2);
```

I.e. when appears assignment of variable (const) if then follows `: <class>` then JS runtime must to call method `static [Symbol.hasInstanceStrict](x)` and transfer to there assignable value.

_______________________________________________
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: Proposal to add symbol: "hasInstanceStrict"

Александр Ефремов
Yes, I think that inherited checks it's also good idea.
And regading `has` I agree. Maybe better to call `requireInstance`.

18 февр. 2018 г. 22:03 пользователь "Aleksander Efremov" <[hidden email]> написал:
Yes, I think that inherited checks it's also good idea.
And regading `has` I agree. Maybe better to call `requireInstance`.

18 февр. 2018 г. 21:43 пользователь "Mike Samuel" <[hidden email]> написал:
Must hasInstanceStrict's parameter remain untyped for a guard to complete or is there a top type with an unguarded hasInstanceStrict that these checks bottom out on?

"has" to my ear sounds like it should return a boolean.  Maybe "require."



On Feb 18, 2018 11:30 AM, "Aleksander Efremov" <[hidden email]> wrote:
It’s attempt to provide intermediate layer for implementation of runtime type checking.

```
class PrimitiveNumber {
        static [Symbol.hasInstanceStrict](x) {
                if (typeof x !== ’number’) {
                        throw new TypeError(‘Invalid type’);
                }
        }
}

function sum(a: PrimitiveNumber, b: PrimitiveNumber) {
        return a + b;
}

const c: PrimitiveNumber = sum(1, 2);
```

I.e. when appears assignment of variable (const) if then follows `: <class>` then JS runtime must to call method `static [Symbol.hasInstanceStrict](x)` and transfer to there assignable value.

_______________________________________________
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: Proposal to add symbol: "hasInstanceStrict"

Александр Ефремов
If not all understood me. I offer to call that `symbol` only in process of execution that code. It isn't attempt to create the compiled language -:).

18 февр. 2018 г. 22:04 пользователь "Aleksander Efremov" <[hidden email]> написал:
Yes, I think that inherited checks it's also good idea.
And regading `has` I agree. Maybe better to call `requireInstance`.

18 февр. 2018 г. 22:03 пользователь "Aleksander Efremov" <[hidden email]> написал:
Yes, I think that inherited checks it's also good idea.
And regading `has` I agree. Maybe better to call `requireInstance`.

18 февр. 2018 г. 21:43 пользователь "Mike Samuel" <[hidden email]> написал:
Must hasInstanceStrict's parameter remain untyped for a guard to complete or is there a top type with an unguarded hasInstanceStrict that these checks bottom out on?

"has" to my ear sounds like it should return a boolean.  Maybe "require."



On Feb 18, 2018 11:30 AM, "Aleksander Efremov" <[hidden email]> wrote:
It’s attempt to provide intermediate layer for implementation of runtime type checking.

```
class PrimitiveNumber {
        static [Symbol.hasInstanceStrict](x) {
                if (typeof x !== ’number’) {
                        throw new TypeError(‘Invalid type’);
                }
        }
}

function sum(a: PrimitiveNumber, b: PrimitiveNumber) {
        return a + b;
}

const c: PrimitiveNumber = sum(1, 2);
```

I.e. when appears assignment of variable (const) if then follows `: <class>` then JS runtime must to call method `static [Symbol.hasInstanceStrict](x)` and transfer to there assignable value.

_______________________________________________
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: Proposal to add symbol: "hasInstanceStrict"

Александр Ефремов
In reply to this post by Александр Ефремов
A reasons for this proposal:
1. I don’t want to use static type checking (Flow or TypeScript) because on my experience there more a time spend on the describing all types and etc. than it gives favor.
2. I want to validate API payloads and other sources of data when it’s need.
I’m sure that runtime type checking also will be enough popular because currently exists a lot of community libraries which implement such functionality but they all use different approaches: via own `babel plugin` or need directly call exposed checks functions. And offered by me such intermediate layer can to unify them approaches.



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

Re: Proposal to add symbol: "hasInstanceStrict"

T.J. Crowder-2
On Mon, Feb 19, 2018 at 1:59 PM, Александр Ефремов <[hidden email]> wrote:
>
> A reasons for this proposal:
> 1. I don’t want to use static type checking (Flow or TypeScript)
> because on my experience there more a time spend on the
> describing all types and etc. than it gives favor.
> 2. I want to validate API payloads and other sources of data when
> it’s need.

When [decorators][1] land, provided they land with a means of [decorating functions][2] (either as part of that proposal or as a follow-on), that would do the job, wouldn't it? Using your `sum` example:

```js
@rttc(PrimitiveNumber, PrimitiveNumber)
const sum = (a, b) => {
    return a + b;
};
```

...where `rttc` ("runtime type check") decorates the function such that it does runtime validation of the type of the arguments supplied. (See the issue referenced in the second link above for why I used a function expression rather than declration for `sum`.)

-- T.J. Crowder



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

Re: Proposal to add symbol: "hasInstanceStrict"

Mike Samuel


On Mon, Feb 19, 2018 at 9:25 AM, T.J. Crowder <[hidden email]> wrote:
On Mon, Feb 19, 2018 at 1:59 PM, Александр Ефремов <[hidden email]> wrote:

When [decorators][1] land, provided they land with a means of [decorating functions][2] (either as part of that proposal or as a follow-on), that would do the job, wouldn't it? Using your `sum` example:

```js
@rttc(PrimitiveNumber, PrimitiveNumber)
const sum = (a, b) => {
    return a + b;
};
```

...where `rttc` ("runtime type check") decorates the function such that it does runtime validation of the type of the arguments supplied. (See the issue referenced in the second link above for why I used a function expression rather than declration for `sum`.)
 
+1 for a single mechanisms that enables not just preconditions, but postconditions.

Decorators could also make it super-easy to do things like deprecation warnings and memoization a la python [1]



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

Re: Proposal to add symbol: "hasInstanceStrict"

Александр Ефремов
In reply to this post by T.J. Crowder-2
How do you offer to check a assignment of result of `fetch` to the variable (const) ? For it also exists decorator?

19 февр. 2018 г. 18:25 пользователь "T.J. Crowder" <[hidden email]> написал:
On Mon, Feb 19, 2018 at 1:59 PM, Александр Ефремов <[hidden email]> wrote:
>
> A reasons for this proposal:
> 1. I don’t want to use static type checking (Flow or TypeScript)
> because on my experience there more a time spend on the
> describing all types and etc. than it gives favor.
> 2. I want to validate API payloads and other sources of data when
> it’s need.

When [decorators][1] land, provided they land with a means of [decorating functions][2] (either as part of that proposal or as a follow-on), that would do the job, wouldn't it? Using your `sum` example:

```js
@rttc(PrimitiveNumber, PrimitiveNumber)
const sum = (a, b) => {
    return a + b;
};
```

...where `rttc` ("runtime type check") decorates the function such that it does runtime validation of the type of the arguments supplied. (See the issue referenced in the second link above for why I used a function expression rather than declration for `sum`.)

-- T.J. Crowder



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

Re: Proposal to add symbol: "hasInstanceStrict"

T.J. Crowder-2
On Mon, Feb 19, 2018 at 4:35 PM, Aleksander Efremov <[hidden email]> wrote:
>
> How do you offer to check a assignment of result of `fetch` to
> the variable (const) ? For it also exists decorator?

I'm not sure I know what you mean. If you could provide a code example
of what you're asking about...?

I don't think there is any concept of a decorator on a local
variable/constant that can intercept all assignments to it, no, if
that's what you mean. If you literally mean `fetch`, you could
decorate the `then` callback function, but that wouldn't help with
`await` syntax (which I'd expect to overtake promise syntax fairly
quickly).

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

Re: Proposal to add symbol: "hasInstanceStrict"

Александр Ефремов
In my first example you can found what I mean. There not only function. Just instead of that function can be black box results which we must check on assignment to const.

19 февр. 2018 г. 20:42 пользователь "T.J. Crowder" <[hidden email]> написал:
On Mon, Feb 19, 2018 at 4:35 PM, Aleksander Efremov <[hidden email]> wrote:
>
> How do you offer to check a assignment of result of `fetch` to
> the variable (const) ? For it also exists decorator?

I'm not sure I know what you mean. If you could provide a code example
of what you're asking about...?

I don't think there is any concept of a decorator on a local
variable/constant that can intercept all assignments to it, no, if
that's what you mean. If you literally mean `fetch`, you could
decorate the `then` callback function, but that wouldn't help with
`await` syntax (which I'd expect to overtake promise syntax fairly
quickly).

-- T.J. Crowder

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

Re: Proposal to add symbol: "hasInstanceStrict"

Mike Samuel


On Mon, Feb 19, 2018 at 11:50 AM, Aleksander Efremov <[hidden email]> wrote:
In my first example you can found what I mean. There not only function. Just instead of that function can be black box results which we must check on assignment to const.

Are you referring to the use of PrimitiveNumber in `const c: PrimitiveNumber = sum(1, 2);` ?
When treating sum as a black box, you can't rely on it to check its {pre,post}condiitons, so you need an additional guard on the const initializer?

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

Re: Proposal to add symbol: "hasInstanceStrict"

Александр Ефремов
Yes, that's it. 

19 февр. 2018 г. 20:53 пользователь "Mike Samuel" <[hidden email]> написал:


On Mon, Feb 19, 2018 at 11:50 AM, Aleksander Efremov <[hidden email]> wrote:
In my first example you can found what I mean. There not only function. Just instead of that function can be black box results which we must check on assignment to const.

Are you referring to the use of PrimitiveNumber in `const c: PrimitiveNumber = sum(1, 2);` ?
When treating sum as a black box, you can't rely on it to check its {pre,post}condiitons, so you need an additional guard on the const initializer?


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

Re: Proposal to add symbol: "hasInstanceStrict"

T.J. Crowder-2
In reply to this post by Александр Ефремов
On Mon, Feb 19, 2018 at 4:50 PM, Aleksander Efremov <[hidden email]> wrote:
> In my first example you can found what I mean.

Yes, I looked at your first example (and every other example in the thread) before replying. As it had nothing to do with `fetch`, but you specifically mentioned `fetch` in your message, I assumed you were asking something about `fetch`. If you were talking about the `const` in `const c: PrimitiveNumber = sum(1, 2);`, why say "`fetch`"?

-- T.J. Crowder

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

Re: Proposal to add symbol: "hasInstanceStrict"

Александр Ефремов
I gave `fetch` you just for example that function result which I want to check. Also on the Node.js I wanted be to check results of `http` module. It just examples of black boxes which impossible to check via decorators how you offered.
`fetch` I assumed to use with `await`. 

19 февр. 2018 г. 20:58 пользователь "T.J. Crowder" <[hidden email]> написал:
On Mon, Feb 19, 2018 at 4:50 PM, Aleksander Efremov <[hidden email]> wrote:
> In my first example you can found what I mean.

Yes, I looked at your first example (and every other example in the thread) before replying. As it had nothing to do with `fetch`, but you specifically mentioned `fetch` in your message, I assumed you were asking something about `fetch`. If you were talking about the `const` in `const c: PrimitiveNumber = sum(1, 2);`, why say "`fetch`"?

-- T.J. Crowder

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

Re: Proposal to add symbol: "hasInstanceStrict"

Александр Ефремов
I also mentioned in my first message that I offered to do call that symbol when appears assignmen of variable or const: when appears creating and assignment parameters of function and also when appears assignment of result function to the variable or const. I just left it in the single example. But in real case instead of sum function can be fetch. Or vice versa we want to check in the our library function passed parameter outside.

19 февр. 2018 г. 21:04 пользователь "Aleksander Efremov" <[hidden email]> написал:
I gave `fetch` you just for example that function result which I want to check. Also on the Node.js I wanted be to check results of `http` module. It just examples of black boxes which impossible to check via decorators how you offered.
`fetch` I assumed to use with `await`. 

19 февр. 2018 г. 20:58 пользователь "T.J. Crowder" <[hidden email]> написал:
On Mon, Feb 19, 2018 at 4:50 PM, Aleksander Efremov <[hidden email]> wrote:
> In my first example you can found what I mean.

Yes, I looked at your first example (and every other example in the thread) before replying. As it had nothing to do with `fetch`, but you specifically mentioned `fetch` in your message, I assumed you were asking something about `fetch`. If you were talking about the `const` in `const c: PrimitiveNumber = sum(1, 2);`, why say "`fetch`"?

-- T.J. Crowder

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

Re: Proposal to add symbol: "hasInstanceStrict"

Александр Ефремов
In reply to this post by Александр Ефремов
And still one question about `function decorators` . They will be work if in function parameters uses destructuring?
```
@rttc(PrimitiveNumber, PrimitiveString, PrimitiveBoolean)
function a (b, { c, d }) {}
```






-------- Исходное сообщение --------
От: "T.J. Crowder" <[hidden email]>
Время: пн 19/2 20:58
Кому: Aleksander Efremov <[hidden email]>
Копия: es-discuss <[hidden email]>
Тема: Re: Proposal to add symbol: "hasInstanceStrict"

On Mon, Feb 19, 2018 at 4:50 PM, Aleksander Efremov <[hidden email]> wrote:
> In my first example you can found what I mean.

Yes, I looked at your first example (and every other example in the thread) before replying. As it had nothing to do with `fetch`, but you specifically mentioned `fetch` in your message, I assumed you were asking something about `fetch`. If you were talking about the `const` in `const c: PrimitiveNumber = sum(1, 2);`, why say "`fetch`"?

-- T.J. Crowder

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

Re: Proposal to add symbol: "hasInstanceStrict"

Александр Ефремов
And still one question about `function decorators` . They will be work if in function parameters uses destructuring?

```
@rttc(PrimitiveNumber, PrimitiveString, PrimitiveBoolean)
function a (b, { c, d }) {}
```

20 февр. 2018 г. 0:13 пользователь "mr.efrem" <[hidden email]> написал:
And still one question about `function decorators` . They will be work if in function parameters uses destructuring?
```
@rttc(PrimitiveNumber, PrimitiveString, PrimitiveBoolean)
function a (b, { c, d }) {}
```






-------- Исходное сообщение --------
От: "T.J. Crowder" <[hidden email]>
Время: пн 19/2 20:58
Кому: Aleksander Efremov <[hidden email]>
Копия: es-discuss <[hidden email]>
Тема: Re: Proposal to add symbol: "hasInstanceStrict"

On Mon, Feb 19, 2018 at 4:50 PM, Aleksander Efremov <[hidden email]> wrote:
> In my first example you can found what I mean.

Yes, I looked at your first example (and every other example in the thread) before replying. As it had nothing to do with `fetch`, but you specifically mentioned `fetch` in your message, I assumed you were asking something about `fetch`. If you were talking about the `const` in `const c: PrimitiveNumber = sum(1, 2);`, why say "`fetch`"?

-- T.J. Crowder

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