microsoft / microsoft/typespec
[core]: Program-bound state variables.
- 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
Assessment
This issue has not been assessed yet.