-
Notifications
You must be signed in to change notification settings - Fork 4.1k
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
[Proposal] typeof as C++11 decltype #3281
Comments
Use type aliases for that. |
@dimaaan I know about aliases, but more clear way will be to use typeof. |
@KalitaAlexey More clear way to do what?
|
I agree. This doesn't appear to solve anything that aliasing doesn't already solve. You example is doubly confusing since you have both a field and a parameter named |
I would like to add that template <class T, class U>
auto add(T x, U y) -> decltype(x + y) {
decltype (x + y) result = x + y;
return result;
} In other words, This could be a very useful feature in a language like C#, not that I am proposing it, but the title of this proposal is misleading as it does not illustrate that. |
That is definitely a more compelling scenario, but I'm still not sure it makes much sense for C# given the lack of compile-time templates. |
@HaloFour I agree, and certainly in C++, decltype is primarily used in conjunction with templates. I just do not want template <class T, class U>
auto add(const T& x, const& U y) -> decltype(x + y) {
decltype (x + y) result = x + y;
return result;
}
|
Can I add that this would be incredibly useful for keeping foreign key ref types in sync in EF and for filter model classes in MVC that reference EF types. See #7715 where I explained in more detail. Also worth mentioning that I am using type aliases in the meantime and it is a lot more work in a large project with a lot of entities. It also makes a mess of the project level reference lists and it needs to be set up separately in all relevant projects, which is far from ideal. See also http://stackoverflow.com/questions/34457028/declaring-a-property-as-the-same-type-as-a-specific-other-property/34457813#34457813 for more discussion. |
That's what #3993 intends to fix. |
Here's a less advanced example representing something programmers run into every day in C#: var foo = whatever();
// the compiler already knows the type of foo, why should I have to specify it?
var asdf = new List<decltype(foo)>();
asdf.Add(foo); If you wanted to do the above today, you'd have to write something like a wrapper method taking a dummy parameter, e.g. private static List<T> MakeListOf<T>(T ignored) {
return new List<T>();
}
var foo = whatever();
var asdf = MakeListOf(foo);
asdf.Add(foo); The wrapper method usually gets obscured by making it do other stuff, i.e. private static List<T> MakeListFrom<T>(T value) {
var ret = new List<T>();
ret.Add(value);
return ret;
}
var foo = whatever();
var asdf = MakeListFrom(foo); Which is how current C# produces the illusion that this feature isn't necessary. |
var asdf = new [] { foo }.ToList(); |
//Converting an enum value to its underlying type value The sad part is Enum.GetUnderlyingType() is only evaluated at runtime. I think this points to the lack of compile time computation in C# |
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 am not moving this particular issue because I don't have confidence that the LDM would likely consider doing this. |
Sometimes I want to write something like
or
Let's on compilation real type will be substituted
or
The text was updated successfully, but these errors were encountered: