Skip to content

datatypevoid/wren-enum

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

29 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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