GithubHelp home page GithubHelp logo

extended_types's Introduction

extended_types

More powerful Lua types.

A patch for the builtin type function for lua. Simply put, this type function attempts to checks for the existence of a .type or .__type value or function. This avoids conflicts with standard lua types, but allows tables to opt-in to being treated like a new type. Since it can be a value OR function, the return values can be anything, but the general usage is to ultimately return a string.

Order of operations is as follows:

  • If it's not userdata or a table, then return the type. This handles standard lua types.
  • If it's userdata, then we check for a type method, and return the return value of said method. This handles LÖVE2D types.
  • If it's a table, we check for a __type metavalue or metafunction. If it's a function, we return the value got from running the function, otherwise, we just return whatever the value was.

The following example uses Classy and LÖVE2D to show how you might do such a thing.

Classy

local Object, Point, p

Object = require("Classy")
Point = Object:extend()

function Point:new(x, y)
    self.x = x or 0
    self.y = y or 0
end

Point.__type = "point"

p = Point(10, 20)

print(type(p)) -- "table"

require("extended_types")

print(type(p)) -- "point"

LÖVE2D

local img = love.graphics.newImage("test.png")

print(type(img)) -- "userdata"

require("extended_types")

print(type(img)) -- "point"

extended_types's People

Contributors

jumpsplat120 avatar

Watchers

 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.