Unmanaged Constants #688
Replies: 24 comments
-
This could cover these scenarios too? BigInteger bint = 420000000000000000000000000000000bi;
Int128 octaword = 12800000000ow; |
Beta Was this translation helpful? Give feedback.
-
It would not cover custom C# literal expressions, so you would still need to declare: BigInteger bint = new BigInteger()
Int128 octaword = new Int128() This just covers being able to emit constants (runtime literals) for blittable structures. |
Beta Was this translation helpful? Give feedback.
-
Its probably worth noting that if, for some reason, there are memory safety concerns/etc, allowing users to declare byte array constants is likely useful in itself (and users could then use the There is also a separate section to these parts that may be useful, which is allowing users to declare their own |
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
@jaredpar, I think you had specifically mentioned that some people were interested in an existing mechanism for embedding UTF-8 strings, could you tag them as appropriate? |
Beta Was this translation helpful? Give feedback.
-
Tagging @KrzysztofCwalina who may be interested |
Beta Was this translation helpful? Give feedback.
-
This assumes that the machine is little endian. It won't work on bigendian machines. |
Beta Was this translation helpful? Give feedback.
-
@jkotas, string literals and all other data embedded in the PE file are also stored in little endian format. I'm sure the distinction is important in some scenarios if/when CoreCLR even properly supports running on big-endian platforms. However, last I checked, we still had multiple algorithms that didn't take big-endian into consideration and had even decided to drop comments indicating that they would need to be updated to support such systems in some cases. Storing as byte-arrays would never correctly work on big-endian platforms. However, I think for the case of supporting a much requested feature and ensuring it is performant, storing and referencing as little-endian is reasonable. However, I'll leave the ultimate decision up to the language-design team. |
Beta Was this translation helpful? Give feedback.
-
The runtime understands these and can swap them as necessary. They are not raw data, like in your example.
Mono does support running on big-endian platforms. We have been fixing the code shared with Mono as necessary to be compatible with bigendian, e.g.: dotnet/corefx@c3d802a |
Beta Was this translation helpful? Give feedback.
-
@jkotas, do you know if the runtime also understands and can swap GrammarThe grammar for them is defined as:
Accessing DataAccessing the data is then defined as:
ExampleAn example given is that you can declare:
A static variable is then declared for the data:
You can then load the variable:
Additional DetailsOne of the interesting scenarios from the grammar is that you can declare structured data:
The static field accessing the data:
Loading the data:
This enables data which is known at link time to be embedded directly into the PE, incurring no startup cost and allowing it to be referenced in other places which use constants/default parameters (this is assuming that constants are required to be readonly). |
Beta Was this translation helpful? Give feedback.
-
I do not know whether Mono does that. I would expect that it does not do it today. |
Beta Was this translation helpful? Give feedback.
-
@tannergooding, I think it shouldn't automatically convert from constructors to const, I think it should be something of automatic constructors. |
Beta Was this translation helpful? Give feedback.
-
@michaelcheers, I agree that automatically converting constructors probably aren't the best way to support this (this should probably be done 'properly' with |
Beta Was this translation helpful? Give feedback.
-
Is it possible to have a blittable struct that cannot be a constant? in other words, do these requirements overlap each other? If they do, I feel like |
Beta Was this translation helpful? Give feedback.
-
@alrz, I would be worried about that being confused with a struct being |
Beta Was this translation helpful? Give feedback.
-
All I'm saying is that we could take a feature that has a requirement of a type being blittable, so we don't need to bring the bittable concept directly to the language, rather maybe just a paragraph in the spec. |
Beta Was this translation helpful? Give feedback.
-
Updated the OP to use the term "Unmanaged", rather than blittable. Also updated the OP to suggest |
Beta Was this translation helpful? Give feedback.
-
I suppose the That being said, I think when we have this mechanism, we can also optimize array/stackalloc initializers on all non-trivial blittable types as well (regardless if they are marked as |
Beta Was this translation helpful? Give feedback.
-
@tannergooding, I would like to propose public static string GetString() {
var value = new { GivenName = "John", SurName = "Doe" };
readonly string SomeValue = $"{value.GivenName} {value.SurName}";
return SomeValue;
} I'm okay with not being able to declare the value as What's the best way to proceed with this request? I didn't see my exact request as an issue already, but this proposal looks like it's along the lines of what I'm looking for personally. Edit: I know that |
Beta Was this translation helpful? Give feedback.
-
@briandrennan, I believe #188 tracks that feature. |
Beta Was this translation helpful? Give feedback.
-
Ah, so it does. Thanks! |
Beta Was this translation helpful? Give feedback.
-
Hi Guys, sorry if not relevant (not red entire thread) - I was searching for way to define const structs as well, but mainly because I want to tight several constants to a single "unit of meaning". My question is - would it be possible instead of const struct, to have at least const tupples? I.e:
|
Beta Was this translation helpful? Give feedback.
-
Any news about this? |
Beta Was this translation helpful? Give feedback.
-
Would I want to do const date = new DateTime(42);
...
[TestCase(date, true)]
... |
Beta Was this translation helpful? Give feedback.
-
Summary
Provide a general-purpose and safe way for declaring constant values for unmanaged structs.
Motivation
Today, users can only create constants for a limited selection of "primitive" types. However, users often want or need to declare constant values for their own types.
Design
The runtime currently provides support for
Data declarations
[II.16.3.1], which I believe can be used to facilitate this support without requiring changes to the underlying runtime.A user should then be able to declare something similar to the following:
This would get emitted as IL similar to the following:
IL Grammar
The IL grammar for a "data declaration" is:
Accessing the Data
Accessing the data is the defined as:
Additional Details
It was suggested that automatically converting constructors may not be the best and providing some separate syntax for this would be beneficial.
Alternatives
Users can use static-readonly to solve "some" of these problems. However, this incurs a one-time runtime initialization cost (per declaration)
The compiler could provide general use of the
CustomConstantAttribute
to support a sort ofpsuedo-constant
(this is what it does forDateTime
andDecimal
).Using
Field Initializers
was originally suggested but this may have issues with machines using different endian formats.Data Declarations
There is also another relevant section
Data declarations
[II.16.3.1]. Specifically, this would allow users to embed data into an assembly that the runtime could reference (both constant and modifiable data). An interesting scenario where this could be used is UTF8 string-literals.Beta Was this translation helpful? Give feedback.
All reactions