-
Notifications
You must be signed in to change notification settings - Fork 4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Will C# ever implement these type annotation features from typescript? #11848
Comments
|
The short answer is "yes". |
#.1 I don't see why "Intersection Types" would require change to CLR. It is possible now with #.5 It only makes sense with interfaces from #.1. We should really give them another name... |
#.4 I see two usages. |
Interfaces are a very different beast in the CLR from what they are in TypeScript. Even if the compiler could just generate a new composite interface extending the intersection interfaces, no type would actually satisfy that generated interface. It's not sufficient to have the same members, implementing types must actually declare that they implement the interface and fill in the required v-table slots. public interface IFoo {
void Foo();
}
public interface IBar {
void Bar();
}
public class FooBar : IFoo, IBar {
public void Foo() { }
public void Bar() { }
}
public interface IFooBar : IFoo, IBar { }
var foobar = new FooBar();
IFooBar duck = foobar; // compiler error |
|
Representing it in C# isn't the problem, it's representing it in the CLR. That "interface" can't actually exist as an interface today. You couldn't declare parameters or generic arguments of that "interface". The C# compiler could fake it within the confines of a method body but that would be about it. I'm not arguing that these features shouldn't be implemented. I like intersection types (less keen on union types *, but I'm not opposed to the idea, just like to see how it would work). My opinion is that if they are to be done that they should be done right, including proper support for them in the CLR so that they can be used anywhere normal types can be used and without a performance penalty. * I do think that union types would be great for exception handlers: class Exception1 : Exception {
string Foo { get; }
}
class Exception2 : Exception {
string Foo { get; }
}
try { ... }
catch (Exception1 | Exception2 exception) {
var foo = exception.Foo; // can reference members common to both interfaces here
...
} I also think that both intersection and union types would be great in pattern matching, either implemented as proper types or as special patterns. |
Union types are also widely used. In StackExchange.Redis, a Task<bool> StringSetAsync(RedisKey key, RedisValue value)
Task<bool> StringSetAsync(string | byte[] key, RedisValue value)
Task<bool> StringSetAsync(string | byte[] key, string | int | int? | bool | bool? /* ... */ value) While public class RedisValue
{
public static implicit operator RedisValue(int value);
public static implicit operator RedisValue(int? value);
// more implicit operators...
} What is left is Intellisence. Intellisence does not show you possible implicitly convertible types of a function parameter. This makes it hard to discover what can be passed to a function when there is an implicit conversion. It can be made to do so without much effort: |
1 related #9595 |
I'm just a little curious. why all these features already are supported in F#, and run in the same CLR? |
@rexxiang F# does things differently and willing to go pretty far in order to give the developer what they want regardless to whether there's CLR support for it so they do pretty funky things to get things done whereas C# is more conservative in that features that require no CLR support would be prioritized first (at least from what I've seen) and features that do require CLR support might be deferred for when CLR changes are pushed and allow feature X to be implemented more efficiently and effectively with no hacks/workarounds to circumvent the CLR, all this regardless to whether it's useful enough, solve a chunk of problems and make sense to the theme of the language. Sometimes features require more discussions and stuff so these may also get pushed to some other version of the language. Finally F# came after C# this allowed the design team more freedom and also to make some design changes that in C# would be a breaking change and last but not least C# and F# are two different languages so some features might not be available to C# or vice-versa regardless to the CLR. I'm just a peon here so this is all just my own observation. 😄 |
We are now taking language feature discussion in other repositories:
Features that are under active design or development, or which are "championed" by someone on the language design team, have already been moved either as issues or as checked-in design documents. For example, the proposal in this repo "Proposal: Partial interface implementation a.k.a. Traits" (issue 16139 and a few other issues that request the same thing) are now tracked by the language team at issue 52 in https://github.com/dotnet/csharplang/issues, and there is a draft spec at https://github.com/dotnet/csharplang/blob/master/proposals/default-interface-methods.md and further discussion at issue 288 in https://github.com/dotnet/csharplang/issues. Prototyping of the compiler portion of language features is still tracked here; see, for example, https://github.com/dotnet/roslyn/tree/features/DefaultInterfaceImplementation and issue 17952. In order to facilitate that transition, we have started closing language design discussions from the roslyn repo with a note briefly explaining why. When we are aware of an existing discussion for the feature already in the new repo, we are adding a link to that. But we're not adding new issues to the new repos for existing discussions in this repo that the language design team does not currently envision taking on. Our intent is to eventually close the language design issues in the Roslyn repo and encourage discussion in one of the new repos instead. Our intent is not to shut down discussion on language design - you can still continue discussion on the closed issues if you want - but rather we would like to encourage people to move discussion to where we are more likely to be paying attention (the new repo), or to abandon discussions that are no longer of interest to you. If you happen to notice that one of the closed issues has a relevant issue in the new repo, and we have not added a link to the new issue, we would appreciate you providing a link from the old to the new discussion. That way people who are still interested in the discussion can start paying attention to the new issue. Also, we'd welcome any ideas you might have on how we could better manage the transition. Comments and discussion about closing and/or moving issues should be directed to #18002. Comments and discussion about this issue can take place here or on an issue in the relevant repo. |
I'm just curious, whether some of TypeScript's type annotation features were already considered as candidates for C#.
One may consider them to be nice typescript features another as necessary evil in order to bring some type safety to javascript.
What do you think about these?
1. interfaces ala TypeScript:
TypeScript example
C# example
this could be useful especially in combination with anonymous types:
it could also help with intellisense when writing anonymous types:
IPerson person = new { F...
- autocompletion listfoo(new { FirstName="Lorem", ...
- autocompletion list2. Intersection types
TypeScript example
C# example
3. Union types
TypeScript example
C# example
4. String Literals (and other literals)
TypeScript example
function animate(speed: "slow" | "fast");
C# example
string literals could be usefull to annotate proxy classes defined by 3rd party. For example, when REST service returns string values like "OK", "NOT OK", then enums cannot be effectively used.
5. Anonymous Interfaces
TypeScript example
let entities = new Array<{ id: number}>();
C# example
The text was updated successfully, but these errors were encountered: