Lombiq/Hastlayer-SDK

Add support for returning an array field (or its clone) from methods (HAST-258)

sarahelsaig opened this issue · 1 comments

As mentioned here:

[...] if you return an array from a method instead of a property it won't work even if the two are functionally equivalent. For example Quire.Segments is an array returning property which is not suggested for performance reasons. But if I replace it with a ulong[] GetSegments() => _segments; method, I will get the The length of the array holder System.Void Lombiq.Arithmetics.Quire::.ctor(System.UInt64[],System.UInt16).Lombiq.Arithmetics.Quire.GetSegments (@this)[82..87) couldn't be statically determined. error. Checking if a method returns a trivial reference could resolve that problem.

We need:

  • ulong[] GetSegments() => _segments.Clone(); : return the clone of an array backing field from a function.
  • IReadOnlyList<ulong> GetSegments() => _segments; : return the backing field directly as an immutable collection.
  • public IReadOnlyList<ulong> Segments => _segments; : do the same from a get-only property.

Jira issue

Following up here. Yep, we'd need to support those three scenarios indeed. IReadOnlyList<T> should be fairly straightforward since we don't actually need to do anything with it on the hardware, it can be an array (since write access to it is prevented in the .NET level already).