Thanks to visit codestin.com
Credit goes to github.com

Skip to content

921starfish/unit-for-luau

Repository files navigation

Unit for Luau

A minimal Unit type implementation for Luau.

Primarily targets Roblox. It will probably work elsewhere, but it has not been tested on Luau, Lute, or Lune.

What is Unit Type?

Unit type is a type that has exactly one value. It is useful when you need to represent "no meaningful value" in a type-safe way, similar to void in other languages but as an actual value.

Installation

Wally

Add the following to your wally.toml:

[dependencies]
Unit = "921starfish/[email protected]"

Note

Due to a Wally limitation, you need to run Wally Package Types Fixer to use the Unit.Unit type.

GitHub

Download src.zip from the latest release and place it wherever you like.

Usage

local Unit = require(path.to.Unit)

-- Create a Unit value
local unit = Unit.new()

-- Or use pre-created Unit value
local unit = Unit.unit

-- All Unit values are equal
print(Unit.new() == Unit.unit) -- true

-- String representation
print(tostring(unit)) -- "()"

Type Annotation

local Unit = require(path.to.Unit)

type Unit = Unit.Unit

local function doSomething(): Unit
    -- do something...
    return Unit.new()
end

API

Functions

Unit.new(): Unit

Returns a Unit value.

Unit.New(): Unit

Alias for Unit.new().

Values

Unit.unit: Unit

A Unit value.

Unit.Unit: Unit

Alias for Unit.unit.

Types

Unit.Unit (Type)

The Unit type for type annotations.

License

MIT