typeof() used where a explicit type is expected #1294
Replies: 9 comments
-
Can you prove your claim? you just proposed a new syntax. but you should compare that with alternative using current c# code. In other words, I don't really get what you mean by
whats the purpose of casting to type of And for |
Beta Was this translation helpful? Give feedback.
-
I guess the proposal is about removing duplication. If you declare that Oracle PL/SQL has a related feature, %TYPE attribute that allows you to declare a type to be the same as another thing (e.g. column) |
Beta Was this translation helpful? Give feedback.
-
It sounds like you want to reference the types of type members. Generics already provide this to a large extent. For example, a customer can implement interface IOrderBearer
{
IEnumerable<T> Orders { get; }
} Of course this gets tedious very quickly if you want to map every property, but the only way to avoid that is to have structural types. Structural types can be incredibly useful but alas, the CLR doesn't have such a concept which would make adding it to C# difficult for the same reason that adding intersection types and union types is difficult. |
Beta Was this translation helpful? Give feedback.
-
Also, I think the last example would be better if you could do: public IEnumerable<ContactJson> ItemsSource {
get {
return (decltype(return))GetValue(ItemsSourceProperty);
}
} which signals to the compiler to look at the return type. Or: public IEnumerable<ContactJson> ItemsSource {
get {
return (decltype(var))GetValue(ItemsSourceProperty);
}
} which signals to the compiler to insert the proper cast for you. |
Beta Was this translation helpful? Give feedback.
-
I don't think its about casting. |
Beta Was this translation helpful? Give feedback.
-
Not always helpful but for single cs file, using alias maybe helpful.
Its not something I would do normally as long as I know that a type is never going to change. but its there you can use it. (I may use it to shorten type names if actual type name is too long.) would be nice if aliases could become public, that would address your problem too. |
Beta Was this translation helpful? Give feedback.
-
That's really your argument for a new feature, "why not?" For one, it's hard to read and is even longer to type, two, customer id could be It doesn't make things easier to read, because now I have to think and remember what that certain type was, which is just unnecessary cognitive load and if I'm trying to refactor something I don't want to have to try and remember what type I was using. |
Beta Was this translation helpful? Give feedback.
-
I've recently run into situations where this would be really useful as well. Something like this can make code refactoring-safe as you can change a type in one place and have it ripple through your source. |
Beta Was this translation helpful? Give feedback.
-
First of all, the way it currently is is not "duplication of type declarations." We're literally talking about declaring the type information of 2 different members on 2 separate objects. You may say it's explicit, and I'd gladly agree with that, but you can't really say it's duplication. If one wants such a coupling between models, one should introduce their own domain models to hold data, so that they'll only need to modify the contents of their |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
I would like to declare fields, parameters and variable using
typeof
, which reduces duplication of type declarations, addtionally makes refactoring easier.For example:
Basically, why not allow
typeof(...)
to be used anywhere a type is expected by the compiler?Another example, casting:
Note the use of
(typeof(this.ItemsSource))
as the cast in the getterBeta Was this translation helpful? Give feedback.
All reactions