Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Operator overload #1

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

JonathanOrr
Copy link

This pull request aims to add operator-overloading to the classes created by the original library

The usage example of the added feature is as follows:

    require "class"

    Vector2 = Class:extend()
    function Vector2:init(x, y)
        self.x = x
        self.y = y
    end

    -- attributes --
    Vector2:set{x = 0, y = 0}

    -- operator overloading --
    function Vector2:__add(other)
        return Vector2(
            self.x + other.x,
            self.y + other.y
        )
    end

    function Vector2:__tostring()
        return string.format("Vector2(%f, %f)", self.x, self.y)
    end

    -----------------------------------------------------------
    local vec1 = Vector2(1, 2)
    local vec2 = Vector2(3, 4)
    print(vec1 + vec2)

The output of the example above is:

    "Vector2(4.0, 6.0)"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant