feat(core): accept readonly vectors in vec2 operations - #52
Draft
RodrigoHamuy wants to merge 1 commit into
Draft
RodrigoHamuy wants to merge 1 commit into
RodrigoHamuy wants to merge 1 commit into
Conversation
Add RVec2, a readonly Vec2, and use it for every vec2 parameter that is only read from. Output parameters stay mutable Vec2 so the receiving vector can still be written to. Mutable vectors remain assignable to RVec2, so existing callers are unaffected. Callers holding a readonly vector can now pass it directly instead of copying or casting. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Adds
RVec2(Readonly<Vec2>) and uses it for everyvec2parameter that is only read from. Output parameters stay mutableVec2.Opening as a draft to check whether you like the approach before I extend it to the rest of the types.
Why: callers holding a readonly vector (a frozen constant, a value from a readonly struct, or anything typed
as const) currently have to copy or cast it to pass it tovec2.add,vec2.length, and friends, even though those functions never write to it. Encoding that in the signature also documents which arguments are outputs.Compatibility:
Vec2is a tuple, soReadonly<Vec2>only widens the input side. Mutable vectors stay assignable toRVec2, so existing callers are unaffected. Passing a readonly vector asoutbecomes a compile error, which is the intended behaviour.Scope:
vec2only, 44 functions.create,fromValues,set,zero, andfromBufferare unchanged since they have no read-only vector params.Checks:
tsc --noEmitclean, all 1154 tests pass. I also typechecked a temporary fixture confirming readonly inputs are accepted, mutable inputs still work, and a readonlyoutis rejected.Question for maintainers: happy for me to extend this to
Vec3,Vec4,Mat2/Mat2d/Mat3/Mat4,Quat/Quat2,Euler,Polar, andSpherical? Those are more involved thanvec2-- the types are consumed acrossshapes/,geometry/, andik/, where internal helpers thread vectors into each other, so the readonly-ness will cascade a fair way. Also worth settling the naming (RVec2vsReadonlyVec2) before it's repeated a dozen times -- gl-matrix usesReadonlyVec2, so that may be the more familiar choice.next steps
If this seems okay, then we can do the same for all of these:
core --- tuple types that get an
R*aliasVec3→RVec3Vec4→RVec4Quat→RQuatQuat2→RQuat2Mat2→RMat2Mat2d→RMat2dMat3→RMat3Mat4→RMat4Euler→REulerSpherical→RSphericalPolar→RPolarcolor
11.
Color→RColor12.
HSL→RHSLshapes --- tuple types with an
R*alias13.
Box2→RBox214.
Box3→RBox315.
Frustum/FrustumCorners→RFrustum/RFrustumCornersshapes --- object types (readonly via
Readonly<...>on the fields, e.g.RSphere = { readonly center: RVec3; readonly radius: number })16.
Sphere→RSphere17.
Circle→RCircle18.
Plane3→RPlane319.
OBB3→ROBB3This is all done in: