Constants in Generic Types using static abstracts and interfaces #6748
Replies: 2 comments 2 replies
-
Cool idea! A good first step would be to pick (or add) a proper standardized BCL interface to use for this to be a syntactic sugar to, I don't think public class Generated : ILiteralValue<double>
{
public static double Value { get; } = Math.Sqrt(2.0);
} Here the value is computed only once but stays constant. But just in case a malicious programmer would want to change it, each type could simply cache the value itself: public class Vector<T> where T : ILiteralValue<int>
{
static readonly int actualT = T.Value;
} This is the only way the type can ensure it's constant anyway. |
Beta Was this translation helpful? Give feedback.
-
I'm in favor of templates, but generics are probably a bad idea to integrate this feature on. Don't get me wrong, the idea of dynamically creating a type denoting the literal passed onto the generic parameter is clever enough, but only gets you so far as to bypass the limitations posed by the infrastructure. The design looks scary at best, if you evaluate a rough sketch on how it would be integrated into the compiler. That being said, P.S. Possible duplicate of #1315? |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Heads Up
I have no idea what I am doing! I've been researching compilers and the nature of programming languages and have come across the idea of
static abstract
. With some toying around, I realized that values could be "smuggled" through generic parameters:This concept is quite intriguing, suggesting the possibility of C++ like template parameters (which are not types) in C#. For sake of argument, let's say that this could be worth codifying into C# as a language. I, in this case, would propose the following:
I'm just posting this out of intrigue, since this could pose interesting points or possibly just be interesting trivia, but I think it's of worth to note.
Any thoughts? Could this idea be extended to non-constant but literal expressions, or even expressions with dynamic evaluations using a new contextual use of the keyword
dynamic
as inType<dynamic int T>
? Could this even, in the extreme, be taken to allow functions as inputs, as inType<delegate int T(int a)>
?Beta Was this translation helpful? Give feedback.
All reactions