[Proposal] Implicit Type Parameters for Value- and ReferenceTypes #8005
Unanswered
a-usr
asked this question in
Language Ideas
Replies: 1 comment 3 replies
-
Just because it's featured so heavily in your use case: #113 I also think bottom types may work in this case as well: #538 This is how Scala handles such cases, e.g. |
Beta Was this translation helpful? Give feedback.
3 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Preamble
At the time of writing this I was unable to find a proposal quite like my Idea, so I decided to write this myself. If such a Feature or Concept already exists, please do tell me
Intro
Ladies and Gentlemen (and people of other genders),
Today I would like to draw your attention to a little Idea of mine, which, if implemented, could come in quite handy!
You see, sometimes we might want to create a type wich can represent two or more different types! Sounds quite easy right? And honestly, it should be! So lets grab an example!
Here we have a Type wich represents either a value of type TLeft or TRight. Seems easy enough.
But wait, what about our Glorious Pattern matching?
Pattern matching
Using the
switch expression
(Introduced in C#8) and theis operator
we can perform pattern matching.Patterns that we can match against include, but are not limited to:
(as described in the C# Documentation)
As you might have noticed, the last two Patterns mentioned are highlighted. That is because these two are pretty interesting to us.
Type Patterns
As the name suggests, this Pattern allows us to check wether the Runtime Type of an object is compatible with a different Type.
Here a Example:
Breaking down this example, we are effectively getting a variable
box
of an unknown Type (Lets just pretend that we got the value ofbox
from a method call), and then we are matching against a couple of possible types that box could have, treatingbox
differently depending on the type that we actually find.Positional Patterns
Using a Positional Pattern, we can Deconstruct an object, and compare against or extract its contents.
Here a quick Example, taken directly from the msl Documentation:
In this example the
point
is only compared against constant values. By altering this Example we can extract thex
andy
values like so:I think this example doesn't need any more explanation, so lets continue.
How can we use those two Patterns?
You probably are corious why I explained those two Pattern types just now, so before I show you why we want to use them, let me give you another Example, this time about how we can combine these two Patterns:
As you can see, I just used Pattern matching to first determine the Type of the
point
, and then deconstructed thepoint
to match against or extract its Data.So, using this, lets redesign our Original
Either<TLeft, TRight>
design:Redesign
Great, now we can use Pattern matching to get our
Left
orRight
value!But lets take a look at the syntax:
As you can see, even though e.g.
Left
only needs to knowTLeft
and notTRight
we need to supplyTright
anyways!Wouldn't the following be much nicer?
So lets try to change our code accordingly:
Enter Implicit Type Parameters
Imagine the following: What if the CLR could automatically infer the omitted type depending on the objects useage?
Here is the concept:
Using Inplicit Type Parameters,
TypeWithLessTypeParameters<TA>
is compatible withITypeWithManyTypeParameters<TA, any>
,where
any
represents every type that satisfies the constraints onTB
set byITypeWithManyTypeParameters<TA, TB>
,or maybe evenTypeWithLessTypeParameters<TA>
Outro
If you have Any Questions, Ideas, additional pieces of Information, or Critique, please feel free to ask or tell!
Beta Was this translation helpful? Give feedback.
All reactions