Skip to content

Immutable/readonly structs (e.g. NSRange) #5110

@Therzok

Description

@Therzok

This is not really a bug, but more of an open discussion about how some things are bound.

Take for example, NSRange: https://github.com/xamarin/xamarin-macios/blob/master/src/Foundation/NSRange.cs#L27

The struct is guidelines are for the struct to be immutable. (https://docs.microsoft.com/en-us/dotnet/standard/design-guidelines/struct)

Cocoa API doesn't seem to let it be mutated either: https://developer.apple.com/documentation/foundation/nsrange?language=objc

In case of structs, when a struct is in a readonly field/property or is passed as arguments to in parameters, the compiler generates a defensive copy.

This happens because this is mutable by default.

C# 7.2 has some nice-ness that allows for structs to be marked as readonly, thus not allowing the this variable to be set, but that requires the property to be fully readonly.

Why was NSRange designed mutable? Might be the case for most structs bound

Metadata

Metadata

Assignees

No one assigned

    Labels

    breaking-changeIf an issue or a pull request represents a breaking changeenhancementThe issue or pull request is an enhancementiOSIssues affecting iOSmacOSIssues affecting macOS

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions