Skip to content

Using in instead of ref? #57

Open
Open

Description

@Happypig375

There are many const T* parameters in Box2D. The easy way is to treat all of them as ref, i.e. not caring about the const part. However, this not only causes unnecessary friction when using the methods, such as the need to declare parameters on a separate line (you can just supply a value to an in parameter), but also loses the readonly intention of the pointer. It may be more correct to expose these pointers as in instead of ref.

Documentation on in vs ref: https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/method-parameters#reference-parameters

Activity

  1. Happypig375 commented on Nov 11, 2025

    @Happypig375
    ContributorAuthor

    Since you translate constant struct pointers that have an associated count to ReadOnlySpan instead of Span, constant struct pointers that only point to one item should be translated as in instead of ref too.

  2. ikpil commented on Dec 14, 2025

    @ikpil
    Owner

    Sorry for the late reply.
    I completely agree with what you mentioned.
    I’ll make the changes that can be adjusted accordingly.

  3. Happypig375 commented on Feb 26, 2026

    @Happypig375
    ContributorAuthor

    It seems that some functions that only take readonly parameters, like b2CollideCircles are now a mix of in and ref, which is weird. I thought that const T* meant in and regular parameters stay as regular parameters.

    public static B2Manifold b2CollideCircles(ref B2Circle circleA, in B2Transform xfA, ref B2Circle circleB, in B2Transform xfB)

  4. Happypig375 commented on Feb 26, 2026

    @Happypig375
    ContributorAuthor

    So inconsistent... Some Id parameters use in and others not. Do you really need to pass a B2ShapeId which is the size of a long by ref? The resulting size is the same on 64-bit operating systems but now you have indirection. Image

  5. ikpil commented on Feb 28, 2026

    @ikpil
    Owner

    Thank you for letting me know. If anything is missing, please let me know. I will review it and make the necessary updates.

  6. ikpil commented on Feb 28, 2026

    @ikpil
    Owner
  7. Happypig375 commented on Feb 28, 2026

    @Happypig375
    ContributorAuthor

    It is also for B2Polygon in B2Manifolds.cs.

  8. ikpil commented on Apr 5, 2026

    @ikpil
    Owner

    I applied this change, but I’m getting a compiler warning.
    The issue occurs when accessing an internal array from a readonly struct passed with in.

    Although the struct is treated as readonly, arrays are reference types and their elements remain mutable, which causes the compiler warning.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions