dotnet / dotnet/macios

Immutable/readonly structs (e.g. NSRange)

Open
#5,110 6 comments 0 reactions 0 assignees View on GitHub
breaking-change enhancement iOS macOS
Dominant language
C#
Stars
2.9k
Forks
576
Avg merge
2d 12h
Merged PRs (30d)
123

Description

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

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.