Giter Club home page Giter Club logo

wren-enum's Introduction

wren-enum

This module simulates enumerated types in wren.

It takes inspiration from the Python Enum API.

Getting Started

The source files should be dropped into an existing project and the top module imported:

import "./relative/path/to/wren-enum/module" for Enum

Alternatively, if utilizing wrenpm for package management in your project, you can add wren-enum to your package.toml file and install wren-enum from within your project root directory with:

$ wrenpm install

Usage

import "./relative/path/to/wren-enum/module" for Enum

// Create an `Enum` from a `List`, automatically generating the value
// of each member.
var Color = Enum.new("Color", ["Red", "Green", "Blue"])

// Output: `<enum 'Color'>`
System.print(Color.toString())

// Output: `<Color.Red: 0>`
System.print(Color["Red"].toString())

// Output: `Red`
System.print(Color["Red"].name)

// Output: `0`
System.print(Color["Red"].value)

// Output: `true`
System.print(Color["Red"] != Color["Blue"])

// Error: `'Enum' does not support the '<' operator.`
// System.print(Color["Red"] < Color["Blue"])

// Output: `true`
System.print(Color[0] == Color["Red"])

// --------------------------------------------------------------------

// Define numeric values for each member in a `Map`.
var City = Enum.new("City", {
  "Portland": 0,
  "Denver": 1,
  "Los Angeles": 2
})

// Output: `<enum 'City'>`
System.print(City.toString())

// Output: `<City.Portland: 0>`
System.print(City["Portland"].toString())

// Output: `Portland`
System.print(City["Portland"].name)

// Output: `0`
System.print(City["Portland"].value)

// Output: `true`
System.print(City["Portland"] != City["Denver"])

// Error: `'Enum' does not support the '<' operator.`
// System.print(City["Portland"] < City["Denver"])

// Output: `true`
System.print(City[0] == City["Portland"])

// --------------------------------------------------------------------

var Flag = Enum.new("Flag", {
  "Read": 1 << 0,
  "Write": 1 << 1
})

// Output: `<enum 'Flag'>`
System.print(Flag.toString())

// Output: `<Flag.Read: 1>`
System.print(Flag["Read"].toString())

// Output: `Read`
System.print(Flag["Read"].name)

// Output: `1`
System.print(Flag["Read"].value)

// Output: `true`
System.print(Flag["Read"] != Flag["Write"])

// Error: `'Enum' does not support the '<' operator.`
// System.print(Flag["Read"] < Flag["Write"])

// Output: `true`
System.print(Flag[1 << 0] == Flag["Read"])

// --------------------------------------------------------------------

// Member values can be `String`s as well.
var Alias = Enum.new("Alias", {
  "Spiderman": "Peter Parker",
  "Ironman": "Tony Stark",
  "Hulk": "Bruce Banner"
})

// Output: `<enum 'Alias'>`
System.print(Alias.toString())

// Output: `<Alias.Spiderman: Peter Parker>`
System.print(Alias["Spiderman"].toString())

// Output: `Spiderman`
System.print(Alias["Spiderman"].name)

// Output: `Peter Parker`
System.print(Alias["Spiderman"].value)

// Output: `true`
System.print(Alias["Spiderman"] != Alias["Ironman"])

// Error: `'Enum' does not support the '<' operator.`
// System.print(Alias["Spiderman"] < Alias["Ironman"])

// Output: `true`
System.print(Alias["Peter Parker"] == Alias["Spiderman"])

Dependencies

  • wren - The best way to get wren up and running on your machine is to build from source. You can find more details here.
  • git - Get git from here.

Testing

Test scripts utilize the wren-test framework and are stored in the tests/ directory. You can launch the tests with:

$ wren ./tests/module.wren

Note that you must have the wren-test framework installed for the tests to run. The fastest way to do this is to build wrenpm and do:

# from within the root directory of this project:
$ wrenpm install

Examples

Examples live in the examples/ directory. You can run an example with:

# `file` is the filename of the example you'd like to run.
$ wren ./tests/file.wren

Wren

Use a Wren-aware editor

We have good experience using these editors:

Versioning

We use SemVer for versioning. For the versions available, see the releases on this repository.

Authors

  • David Newman - Initial development and ongoing maintenance - datatypevoid

See also the list of contributors who participated in this project.

License

This project is licensed under the ISC License - see the LICENSE file for details

Acknowledgments

wren-enum's People

Contributors

datatypevoid avatar

Stargazers

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

Watchers

 avatar  avatar

wren-enum's Issues

Can initialize Enum with List containing duplicate values

This doesn't really cause any problems per se (because of the way List inputs are transformed to Maps), but it should throw an error regardless in my opinion.

Something like this should be invalid input:

[
  "Red",
  "Blue,
  "Green",
  "Red"
]

Ability to check if an Enum instance has a member associated with a key/value

Currently the only way to check if an Enum has a member associated with a key/value is to attempt to index the Enum and handle the error if an invalid index is given (ie no member associated). Better would be if the Enum checked and handled the error so we aren't managing extra Fibers everywhere.

Something along the lines of:

var hasMember = enum.has(index) // Return a `Bool`

is what I was thinking.

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.