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

Vector2 and Vector3 should implement __idiv #153

Closed
osyrisrblx opened this issue Feb 13, 2024 · 7 comments · Fixed by #196
Closed

Vector2 and Vector3 should implement __idiv #153

osyrisrblx opened this issue Feb 13, 2024 · 7 comments · Fixed by #196
Labels
enhancement New feature or request good first issue Good for newcomers roblox Issues pertaining to the built-in Roblox library

Comments

@osyrisrblx
Copy link

osyrisrblx commented Feb 13, 2024

Vector2 and Vector3 currently do not support the // operator in Lune, but they do in Roblox.

Should allow:

  • Vector2 // Vector2 -> Vector2
  • Vector2 // number -> Vector2
  • Vector3 // Vector3 -> Vector3
  • Vector3 // number -> Vector3

Pseudo code in Lua:

function Vector2:__idiv(other)
    if type(other) == "number" then
        return Vector2.new(
            math.floor(self.X / other),
            math.floor(self.Y / other)
        )
    else
        return Vector2.new(
            math.floor(self.X / other.X),
            math.floor(self.Y / other.Y)
        )
    end
end

function Vector3:__idiv(other)
    if type(other) == "number" then
        return Vector3.new(
            math.floor(self.X / other),
            math.floor(self.Y / other),
            math.floor(self.Z / other),
        )
    else
        return Vector3.new(
            math.floor(self.X / other.X),
            math.floor(self.Y / other.Y),
            math.floor(self.Z / other.Z),
        )
    end
end
@filiptibell filiptibell added enhancement New feature or request good first issue Good for newcomers roblox Issues pertaining to the built-in Roblox library labels Feb 13, 2024
@CompeyDev
Copy link
Contributor

CompeyDev commented Feb 25, 2024

Floored division isn't a feature of luau, IIRC.

EDIT: Yes, turns out this is true. As per mlua docs, the IDiv metamethod can only be implemented for vanilla lua 5.x.

@CompeyDev
Copy link
Contributor

Considering this is fairly simple to be implemented in pure luau, I question the requirement of having to implement this on Rust-side (i.e., providing a convenience function), especially considering the fact that it wouldn't integrate into luau well.

@LouieK22
Copy link

@CompeyDev As I understand Lune's Vector datatypes, they should support the same API surface as Roblox's Vector datatypes (https://github.com/lune-org/lune/blob/main/src/roblox/datatypes/types/vector3.rs#L19-L20). While not documented on their official docs, Roblox does support integer division for both Vector datatypes, so Lune should as well:
image

@CompeyDev
Copy link
Contributor

We would need to implement a custom metamethod or something similar, no clue how that could be approached in mlua. Will update if there is a straightforward way to do it.

@LouieK22
Copy link

Presumably this should be escalated to an issue in mlua? The luau spec specifies that Luau supports the __idiv metamethod.

@CompeyDev
Copy link
Contributor

You're correct, I'm able to use floored division in the vanilla luau REPL, but not in lune. That's odd.

@CompeyDev
Copy link
Contributor

I've created an issue: mlua-rs/mlua#383.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request good first issue Good for newcomers roblox Issues pertaining to the built-in Roblox library
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants