Giter Site home page Giter Site logo

stefano-m / lua-enum Goto Github PK

View Code? Open in Web Editor NEW
22.0 3.0 0.0 36 KB

Enumerated Types for Lua

Home Page: https://stefano-m.github.io/lua-enum/

License: Apache License 2.0

Makefile 9.26% Lua 74.66% Nix 16.07%
lua enum enumeration enumerated-types

lua-enum's Introduction

Build Status codecov

Enum @VERSION@ - Simulate Enums in Lua

This is a little module that simulates enumerated types in Lua.

Its API is very similar to the Python3 Enum API, although much more limited.

Example Usage

enum = require("enum")

sizes = {"SMALL", "MEDIUM", "BIG"}
Size = enum.new("Size", sizes)
print(Size) -- "<enum 'Size'>"
print(Size.SMALL) -- "<Size.SMALL: 1>"
print(Size.SMALL.name) -- "SMALL"
print(Size.SMALL.value) -- 1
assert(Size.SMALL ~= Size.BIG) -- true
assert(Size.SMALL < Size.BIG) -- error "Unsupported operation"
assert(Size[1] == Size.SMALL) -- true
Size[5] -- error "Invalid enum member: 5"

-- Enums cannot be modified
Size.MINI -- error "Invalid enum: MINI"
assert(Size.BIG.something == nil) -- true
Size.MEDIUM.other = 1 -- error "Cannot set fields in enum value"

-- Keys cannot be reused
Color = enum.new("Color", {"RED", "RED"}) -- error "Attempted to reuse key: 'RED'"

Installing

Using "classic" nix

If you are on NixOS, you can install this package from nix-stefano-m-overlays.

Using nix flakes

This package can be installed as a nix flake. It provides packages for the various versions of Lua shipped with nix as well as an overlay that adds the enum attribute to the Lua package sets.


NOTE

To ensure that the flake overlays are composable, enum is added directly to the top-level luaPackages. An unfortunate consequence is that enum will not be present in lua.pkgs. To use enum in lua.withPackages, one must refer to the top-level luaPackages.

For example, say that you want a Lua environment with enum and http, you need to do something like:

let
  # assume that enumOverlay is the overlay provided by the enum flake.
  pkgs = import <nixpkgs> {overlays = [enumOverlay];};
in
# http is present in lua.pkgs
# enum is not present in lua.pkgs
myLuaEnv = pkgs.lua.withPackages(ps: [ps.http pkgs.luaPackages.enum])

Using Luarocks

This package is published to Luarocks as enum and can be installed using

luarocks install enum

lua-enum's People

Contributors

stefano-m avatar

Stargazers

 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.