Skip to content

Commit 6950b7f

Browse files
authored
support for delete, shorthand and computed properties, and functions at the end of function (microsoft#5916)
* Add tsconfig.json for easier editing of lang-test0 * Support delete on dynamic maps * Minor doc fixes * Object destruct. was already fully supported * Implement shorthand properties * Support computed property names * Remove generic HOF restriction * Remove move generic restrictions * Allow for function definitions after usage * Explain function scoping * Remove junk
1 parent d8ef9ed commit 6950b7f

10 files changed

Lines changed: 293 additions & 39 deletions

File tree

docs/language.md

Lines changed: 42 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -59,16 +59,16 @@ and might not be available in your editor.
5959
* using generic functions as values and nested generic functions
6060
* binding with arrays or objects: `let [a, b] = ...; let { x, y } = ...`
6161
* exceptions (`throw`, `try ... catch`, `try ... finally`)
62-
63-
The following used to be disallowed, but should be supported now,
64-
though they require testing:
65-
6662
* downcasts of a superclass to a subclass
6763
* function parameter bi-variance
6864
* explicit or implicit use of the `any` type
6965
* `union` or `intersection` types
7066
* using a generic function as a value
7167
* class inheritance for generic classes and methods
68+
* `delete` statement (on object created with `{...}`)
69+
* object destructuring with initializers
70+
* shorthand properties (`{a, b: 1}` parsed as `{a: a, b: 1}`)
71+
* computed property names (`{[foo()]: 1, bar: 2}`)
7272

7373
## Unsupported language features
7474

@@ -80,11 +80,8 @@ Static TypeScript has *nominal typing* for classes, rather than the *structural
8080
* `this` used outside of a method
8181
* function overloading
8282

83-
We generally stay away from the more dynamic parts of JavaScript. Things you may miss and we may implement:
83+
Things you may miss and we may implement:
8484

85-
* object destructuring with initializers
86-
* shorthand properties (`{a, b: 1}` parsed as `{a: a, b: 1}`)
87-
* `delete` statement (on object literals)
8885
* spread and reset operators (statically typed)
8986
* support of `enums` as run-time arrays
9087
* `new` on non-class types
@@ -97,14 +94,41 @@ unlikely to miss it):
9794
* file-based modules (`import * from ...`, `module.exports` etc); we do support namespaces
9895
* `yield` expression and ``function*``
9996
* `await` expression and `async function`
100-
* tagged templates ``tag `text ${expression} more text` ``; regular templates are supported
97+
* tagged templates ``tag `text ${expression} more text` `` are limited to special compiler features
98+
like image literals; regular templates are supported
10199
* `with` statement
102100
* `eval`
103101
* `for ... in` statements (`for ... of` is supported)
104102
* prototype-based inheritance; `this` pointer outside classes
105103
* `arguments` keyword; `.apply` method
106104
* JSX (HTML fragments as part of JavaScript)
107105

106+
Static TypeScript has somewhat stricter ideas of scoping than regular TypeScript.
107+
In particular `var` is not allowed (`let` and `const` are supported),
108+
and identifiers defined with `function` can only be used after all variables
109+
from outer scopes have been defined.
110+
(The closure objects for functions that are used before definition
111+
is constructed right after last used variable have been defined.
112+
For functions defined before usage, the closure is constructed at the
113+
point of definition.)
114+
Both of the following examples will yield a compile error.
115+
```typescript
116+
function foo1() {
117+
bar()
118+
let x = 1
119+
function bar() {
120+
let y = x // runtime error in JavaScript
121+
}
122+
}
123+
function foo1() {
124+
const tmp = bar
125+
let x = 1
126+
tmp() // no runtime error in JavaScript
127+
function bar() { let y = x }
128+
}
129+
```
130+
131+
108132
For JS-only targets we may implement the following:
109133

110134
* regular expressions
@@ -136,6 +160,15 @@ monkey-patch these.
136160
Finally, classes are currently not extensible with arbitrary fields.
137161
We might lift this in future.
138162

163+
`Object.keys(x)` is not yet supported when `x` is dynamically a class type.
164+
It is supported when `x` was created with an object literal (eg., `{}` or `{ a: 1, b: "foo" }`).
165+
The order in which properties are returned is order of insertion with no
166+
special regard for keys that looks like integer (JavaScript has
167+
[really counter-intuitive behavior](https://www.stefanjudis.com/today-i-learned/property-order-is-predictable-in-javascript-objects-since-es2015/)
168+
here).
169+
When we support `Object.keys()` on class types, the order will be the static order of
170+
field definition.
171+
139172
## Execution environments
140173

141174
PXT programs are executed in at least three different environments:
@@ -246,6 +279,4 @@ and dynamic maps.
246279
we could make it equivalent to JavaScript's `x.foo.bind(x)`
247280
* `Object.keys()` is currently not implemented for classes; when it will be
248281
the order of fields will be static declaration order
249-
* the `delete` statement is currently disallowed; it can be implemented
250-
rather easily, though on classes it will just assign `undefined`
251282
* how to validate types of C++ classes (Pin mostly)?

0 commit comments

Comments
 (0)