microsoft / microsoft/typespec

[core]: Program-bound state variables.

Open
#7,274 0 comments 0 reactions 0 assignees View on GitHub
compiler:core feature triaged:core
Dominant language
Java
Stars
5.9k
Forks
394
Avg merge
1d 23h
Merged PRs (30d)
104

Description

We have state maps and state sets. What I'm suggesting is an even simpler primitive which is a state variable.

```ts
const [get, set] = useStateVariable(key, initialValue);

get(program);

set(program, value);
```

I'm currently using a pattern pretty frequently that looks like:

```ts
const KEY = Symbol.for(...);

interface Store {
[KEY]?: T;
}

function get(program): T | undefined {
return (program as unknown as Store)[KEY];
}

function set(program, value) {
(program as unknown as Store)[KEY] = value;
}
```

This lets me ephemerally associate arbitrary state with the Program in a way that simple state sets and maps don't (because they require that the state be associated with a Type).

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.