Giter Site home page Giter Site logo

as-variant's Introduction

Variant Data Type for AssemblyScript

Build Status npm

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

Basic Usage

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<f64>())            // ok
assert(!vStr.is<f64>())           // ok
assert(vStr.is<string>())         // ok
assert(vFoo.is<Foo>())            // ok

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

let valF64   = vNum.get<f64>()    // safely extract value
let willFail = vNum.get<string>() // will throw exception!

Unsafe Usage:

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

Use as value for Any Dynamic Dictionary

const dict = new Map<string, Variant>()

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<i32[]>()[2] == 3)
// or
assert(dict['arr'].get<i32[]>()[2] == 3)

which equivalent to JavaScript:

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

dict.arr = [1, 2, 3]

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

as-variant's People

Contributors

dcodeio avatar jairussw avatar maxgraey avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.