Closed dzmitry-lahoda closed 5 years ago
There are a few problems with this, 1. the CLR generics restriction of not allowing ref Type arguments, and 2. the C# language restriction of also not allowing ref-like (Span etc) type arguments either, mainly because the CLR does not even support the weaker versions.
@NinoFloris , I do not understand what you are saying. May be I can clarify.
Now:
I want:
Question: Is 2.2 unsound/leaky?
As I see you say that 2.2 not possible because of both CLR and C#. But I doubt that CLR has restrictions. Probably C# only restrictions do apply. I just cannot grasp what exactly CLR prevents from your answer.
class Readonly
{
private ReadPosition one;
public ref ReadPosition this[int index] => ref one;
}
class Readonly2
{
private ReadPosition one;
public ref readonly ReadPosition this[int index] => ref one;
}
// compiles - OK
var myReadonly = new Readonly();
ref ReadPosition my = ref myReadonly[0];
my = new ReadPosition();
// does not compiles - BAD
var myReadonly2 = new Readonly2();
ref ReadPosition my2 = ref myReadonly2[0]; //does not compiles, but could
my2 = new ReadPosition();// should be disallowed
So to restate:
readonly
and struct
is readonly
than ref
should be allowed in above case.ref
returned from readonly index
of readonly struct
should prevent write to that ref
. As in this case, readonly
marker on struct
is useless as I can create new struct and replace it by ref, essentially modify readonly struct
.So I guess 2 can be CLR limitation.
ref readonly
. Sorry for that. I have not found answer on SO...
С# could tune error message which suggest to mark ref return as readonly as part of fix so :)
Issue
Returning
readonly struct
fromReadOnlySpan
asref
should be possible.Why I want it
I have several different
classes
. Some of them may modify elements's fields in place, some add/replace elements, some read copy of elements, some obtain readonly ref onto elements. I want make it clear what are capabilities of each of this class to allow write correct code. I want to createproperly typed
sandboxes of developers.I want to have view onto same block of memory(array), not copy. I do data oriented design with zero GC by allocating most of memory on start for my game.
I was inspired by Rust memory management Data Oriented GUI in Rust by Raph Levien.
With C# 8 interfaces with implementation I can do Rust design, I want to emulate Rust memory management either.
I do not want copy large structs to read (10+ properties).
Considerations
I do not understand, why I cannot get ref to readonly struct from readonly span, because I cannot modify that struct anyway, but want to read on its members.
Not sure about non GUI or non ECS cases if any uses. I want to have previous state of
world
readonly provided with new state ofworld
which is read write (in some cases only working with existing elements and in some cases possibility to add/"remove").Sometimes I even may want to be very sloppy allowing concurrent writes and some fields reads from readonly refs to get some inputs into neural network (let bots to be stupid a little for some performance gain). I want to run some of my
classes
in parallel.Example