Add "return self" to the tail of all "set" functions of LuaVec3/LuaQuat in common/mathlib.lua

Discussion in 'Ideas and Suggestions' started by Derpitron, Mar 5, 2026.

  1. Derpitron

    Derpitron
    Expand Collapse

    Joined:
    Jun 25, 2017
    Messages:
    263
    To facilitate concatenative programming, I want LuaVec3 and LuaQuat's "set" member methods to always return `self` (unless it's specifically creating a new copy of the vec3/quat).
    (For the unaware you can read about concatenative/point free programming here: https://stackoverflow.com/a/5671552. If you come from a software engineering background this might be called a "pipeline", "fluent interface", "builder pattern, etc"

    Further, add a "tap" function for all 3 custom type metatables, which basically applies a function then returns the same object, letting you write out-of-band computations and continue the pipieline.
    Code:
    function LuaVec3:tap(f)
        f(self)
        return self
    end
    
    function StackVec3:tap(f)
        f(self)
        return self
    end
    
    
    function LuaQuat:tap(f)
        f(self)
        return self
    end
    Including but not limited to the following examples below. Taken verbatim from the BeamNG lua source code but modified with the following boxes highlighting the change I want.
    upload_2026-3-5_10-20-41.png
    upload_2026-3-5_10-21-1.png
    upload_2026-3-5_10-25-36.png



    upload_2026-3-5_10-23-19.png
    upload_2026-3-5_10-24-27.png
    upload_2026-3-5_10-25-5.png


    This will let me apply transformations like the following, which makes for cleaner readable code that other programmers can read, debug, and reason about easier. (This code currently isn't possible, but if the above changes are implemented it would be possible.)
    upload_2026-3-5_10-33-48.png


    Currently it's not possible to implement this in the scope of a repository-legal game mod. This kind of change requires a change to the LuaVec3/LuaQuat metatable member method definitions within mathlib.lua directly, because it's not possible to modify the metatable from outside that scope, and it's not good practice to override the vanilla game libraries with custom shim code, that would be a maintenance burden and cause conflicts with other mods/code. Having this feature directly in the vec3/quat library would be much easier for me as a Lua programmer.

    Further it's not feasible to implement this in a third-party wrapper function as it would be unergonomic, ugly, and again a maintenance burden to continually keep it updated with the game's lua library updates.

    @stenyak
     

    Attached Files:

    • upload_2026-3-5_10-21-44.png
    • upload_2026-3-5_10-22-28.png
    • upload_2026-3-5_10-22-52.png
    #1 Derpitron, Mar 5, 2026
    Last edited: Mar 5, 2026
  2. Derpitron

    Derpitron
    Expand Collapse

    Joined:
    Jun 25, 2017
    Messages:
    263
    I'm hoping for the mathlib.lua functions to be ported to the flowgraph system too. Flowgraph uses a separate vec3 and quat library, and lacks a lot of the functionality/operators present in the main code
     
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice