compile-time readonly arrays, const T[] #955
Replies: 22 comments
-
This would probably conflict with the most likely syntax for C/C++ has a few different syntaxes for this. Mutable Pointer and Mutable Contents
Mutable Pointer and Immutable Contents
Immutable Pointer and Mutable Contents
Immutable Pointer and Immutable Contents
I would think, if C# were to expose it, we would want a way to expose each level of immutability as well. Mutable Array and Mutable Contents
Mutable Array and Immutable Contents
Immutable Array and Mutable Contents
Immutable Array and Immutable Contents
|
Beta Was this translation helpful? Give feedback.
-
What about a
Why should this be a language feature? Usually, those are reserved for things that can't be done with a library. |
Beta Was this translation helpful? Give feedback.
-
@svick ImmutableArray has boxing and unboxing problem (even though it could be rare), but it cant be done with a library to achieve true readonly array. good to know that such thing exist, I'm going to use them. |
Beta Was this translation helpful? Give feedback.
-
How would these "arrays" be implemented? Without a facility to support such a thing in the CLR at best you'd have some kind of wrapper, but you couldn't treat that wrapper as an array. |
Beta Was this translation helpful? Give feedback.
-
@HaloFour, I would guess, at best, the language would provide some tracking (this is all C/C++ does and is really all C# does for That being said, a struct wrapper is probably good enough for most cases. |
Beta Was this translation helpful? Give feedback.
-
Also initializing ImmutableArray creates garbage in process. the IEnumerable. though single instance of a special IEnumerable could be used where you can change its state to enumerate new data. (and that also uses single enumerator instance) |
Beta Was this translation helpful? Give feedback.
-
@MkazemAkhgary What's the "garbage" |
Beta Was this translation helpful? Give feedback.
-
@yaakov-h |
Beta Was this translation helpful? Give feedback.
-
@HaloFour the same way as readonly keyword is implemented, as I said, it only makes sense at compile time. |
Beta Was this translation helpful? Give feedback.
-
@MkazemAkhgary |
Beta Was this translation helpful? Give feedback.
-
@yaakov-h thank you so much, did not know such thing exist. hmm my arrays are also less that length of 4 so I can really pass parameters without creating any temporary array. |
Beta Was this translation helpful? Give feedback.
-
Under what circumstance? Normally Or are you referring to |
Beta Was this translation helpful? Give feedback.
-
Plus the compiler couldn't rewrite foreach as a for. Note: this is a reduced version of #421. |
Beta Was this translation helpful? Give feedback.
-
I also think that compiler only validation is probably "good enough" for most cases (even with runtime "enforcement", said enforcement can generally be broken via One of the few issues comes up if the user wants to directly expose the array and another language wants to consume it, but that could probably be handled via 'modreq' (in the same manner as |
Beta Was this translation helpful? Give feedback.
-
IMHO it is another proposal going around some sort of value/object/array/references/pointers immutability at runtime and at compile time. For me it amounts to growing community desire to get it sorted out at the level of languages and framework (CIL, Jit). Therefore I would like to propose a bit more generalized solution where C#/VB/F# (F# being obviously the easiest case) would support notion of:
Than declaration of array could look like after @tannergooding : // Mutable Array and Mutable Contents
T[]
// Mutable Array and Immutable Contents
T readonly[]
// Immutable Array and Mutable Contents
readonly T[]
// Immutable Array and Immutable Contents
readonly T readonly[] (or maybe immutable T[])
// Explicit conversion required
T[] = (T[]) readonly T[];
// Mutable Array and Constant Contents
T const[]
// Const Array and Mutable Contents
const T[]
// Const Array and Const Contents
const T const[]
// Explicit conversion fails
// Error: cannot convert const Array to readonly Array - conversion from const to readonly type not allowed
readonly T[] = (readonly T[]) const T[]; From language compiler perspective enforcement of the above syntax is straightforward - one can look at my At IL and runtime level it is possible to use AFAIR there was some recent work in academia on efficient runtime implementations of WriteBarriers for garbage collected languages which support transient immutability universally for all objects at low cost (one bit "flag") and most probably such mechanism could be implemented in dotnet. However the picture gets really complex when you go through the rabbit hole. One first has to solve problems of type equivalence:
imho this is one of the areas in dotnet which is slowly becoming a low hanging fruit worth putting some work into eating it. just my 3 cents |
Beta Was this translation helpful? Give feedback.
-
IIRC IL even doesn't have opcodes to load data from constants becuase they have to be inlined by compiler. Thus Of course, we can emulate it via |
Beta Was this translation helpful? Give feedback.
-
You probably misunderstood. I didnt say const is constant. In this proposal const keyword refers to readonly array. Not constant array Maybe i choosed wrong keyword. The comment below proposal suggests readonly keywords which is good. |
Beta Was this translation helpful? Give feedback.
-
@Pzixel That is obviously not true as usability is a distinct term from runtime support. Significant number of new language features requires runtime changes what has nothing to do with their usability. If you say something is unusable bcs there are no reasonable use cases than it can be at least discussed. |
Beta Was this translation helpful? Give feedback.
-
@Pzixel, the IL has support for a thing called "Data Declarations" which is (essentially) compile time structured data that is consumed by runtime. This could be used to support actual blittable constants (#688) or UTF8 string literals (#909). |
Beta Was this translation helpful? Give feedback.
-
Guess this is never happening then... |
Beta Was this translation helpful? Give feedback.
-
@IanKemp the bed for this seems minimal, esp with the availability of types like ReadOnlySpan. |
Beta Was this translation helpful? Give feedback.
-
We need WPO and parial evaluation to make things smoother. But I think it's outside C# LDM goals. |
Beta Was this translation helpful? Give feedback.
-
Allow arrays marked as
const
will have get only indexer. theset[]
is prevented at compile time, after compilation readonly arrays are exactly as same as normal arrays.This is not something that should be (or even could be) controlled in runtime. This keyword has same semantics of readonly keyword but for elements of array.
though this might require changes in CRL but Imho it worth the feature.
(the syntax is just proposed, but could be anything that describes the behavior.)
ReadOnly collection wrappers always occupy an extra reference, when you have thousands of small byte arrays, having read only wrappers doesn't sound optimal, especially when those bytes are smaller that 8 or 4 bytes, so you drop the readonly wrapper thing from your code to remove extra reference overhead, but then you are not sure of array immutability in your code.
read only wrappers are not language feature, in my opinion c# is really missing readonly array feature.
why not add
const
keyword especial meaning for arrays and call it a day?const T[]
is assignable, but does not have setter (this is prohibited in compile time)const T[]
is not assignable toT[]
T[]
is always assignable toconst T[]
by reference. so as long as there is reference toT[]
changes will also changeconst T[]
.const T[]
can be used in local/field variable declarations, method and delegate parameters, generics and type parametersconst T[]
andref T[]
are mutually exclusive so they cant be used together.readonly const T[]
is readonly by index and by reference.const T[] declaration/initialization and assignments
Fields, const[] and readonly.
const T[] Property:
const T[] method parameters and generic methods
Again iterating over same thing, const[] returns, parameters and generic parameters and inside delegates would be also possible
type parameters
this could be also used inside type parameters when initializing a generic class. because when declaring
T
it does not matter if itsconst int[]
orint[]
. in static code, you are doing operations withT
Beta Was this translation helpful? Give feedback.
All reactions