Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/MaxGraey/as-variant

Variant data type for AssemblyScript
https://github.com/MaxGraey/as-variant

any assemblyscript dynamic type variant wasm webassembly

Last synced: about 1 month ago
JSON representation

Variant data type for AssemblyScript

Awesome Lists containing this project

README

        

Variant Data Type for AssemblyScript
===

[![Build Status](https://github.com/MaxGraey/as-variant/actions/workflows/test.yml/badge.svg?event=push)](https://github.com/MaxGraey/as-variant/actions/workflows/test.yml?query=branch%3Amain)
[![npm](https://img.shields.io/npm/v/as-variant.svg?color=007acc&logo=npm)](https://www.npmjs.com/package/as-variant)

Supports any builtin types like i32, bool, string and any custom classes (managed and unmanaged).

### Basic Usage

```ts
import { Variant } from 'as-variant/assembly'
// before 0.20.x
// import { Variant } from 'as-variant'

class Foo { }

let vNum = Variant.from(123) // stored as i32
let vStr = Variant.from('hello') // stored as string
let vFoo = Variant.from(new Foo) // stored as Foo reference

vNum.set(2.0) // now stored as f64

assert(vNum.is()) // ok
assert(!vStr.is()) // ok
assert(vStr.is()) // ok
assert(vFoo.is()) // ok

assert(vNum.id != vStr.id) // compare dynamic IDs.
assert(vFoo.id == Variant.idof())

let valF64 = vNum.get() // safely extract value
let willFail = vNum.get() // will throw exception!
```

### Unsafe Usage:

```ts
let vNum = Variant.from(123)
// `getUnchecked` skips all checks. It may be danger.
assert(vNum.getUnchecked() == 123)
```

### Use as value for Any Dynamic Dictionary

```ts
const dict = new Map()

dict.set('str', Variant.from('hello'))
dict.set('num', Variant.from(124.0))

dict.set('arr', Variant.from([1, 2, 3]))

assert(dict.get('arr').get()[2] == 3)
// or
assert(dict['arr'].get()[2] == 3)
```

which equivalent to JavaScript:

```js
const dict = {
str: 'hello',
num: 124.0,
}

dict.arr = [1, 2, 3]

assert(dict.arr[2] == 3)
```