C# Next - Feature Request: Multi Casting #2603
Replies: 18 comments
-
This sounds quite similar to #399 |
Beta Was this translation helpful? Give feedback.
-
I think to implement this functionality, the compiler would need to either introduce the extra level of inheritance or generate different code for each case. Also, what type is I think the syntax kinda conflicts with tuples - is |
Beta Was this translation helpful? Give feedback.
-
This does sound like #399, but the underlying issue seems like it could also be addressed with #164. |
Beta Was this translation helpful? Give feedback.
-
that's what i said... it allow user to calculate a matter only once. then, behind the scene compiler remove those hassles (as reflection matters are costly) and provide a hard coded version of that calculation per each type and place it inline... or maybe you as the one who write compiler it self, have access to lower lever functionality that make it easier without doing all those heavy functionality of reflection, and provide it in better ways. |
Beta Was this translation helpful? Give feedback.
-
dynamic is not a dictionary, ExpandoObject is. |
Beta Was this translation helpful? Give feedback.
-
Well i'm not aware of substructures, if i had two ability: knew how to write test, easily read and copy other people codes, i would get more active in GitHub as it could be strong resume for me... But i say that because i had an issue long ago, and i had to do some dynamic property reading... either :
and yes, dynamic is nothing but a keyword, that uses those structural capacities |
Beta Was this translation helpful? Give feedback.
-
No. dynamic is a keyword that defers a lot of stuff that the compiler checks does until runtime. The |
Beta Was this translation helpful? Give feedback.
-
To add a little more detail.... The For example: public void PrintLength(dynamic obj) => Console.WriteLine(obj.Length);
PrintLength("Hello, World");
PrintLength(new int[] { 1, 2, 3 }); The Now, most of the time the type which is referred to by If I pass an object which implements This is mainly meant for interoperability with scripting languages, which tend to be inherently late bound. You can create an object which implements The BCL does ship with two classes which implements |
Beta Was this translation helpful? Give feedback.
-
Also, have you thought about using interfaces? Common practice is to define an interface which holds the properties which are common between your objects, and have both objects implement that interface. Then you have a strong compile-time contract which says that both For example: public interface IContactable
{
string FirstName { get; set; }
string LastName { get; set; }
PersonTitle? OwnerTitle { get; set; }
string ContactEmail { get; set; }
string ContactTel1 { get; set; }
string ContactTel2 { get; set; }
} Then your validation method can take an |
Beta Was this translation helpful? Give feedback.
-
Can't this be solved with a switch expression? |
Beta Was this translation helpful? Give feedback.
-
this is the opposite of switch expression... you naturally filter out, and find data types, and pass them to switch, to write codes which are differences from one another, |
Beta Was this translation helpful? Give feedback.
-
Interfaces for sure are good and made life easy in many places, but we are talking about objects of different types, you do not always access to object of a lower system which you are referencing in your system. also adding an interface or class, sometime, adds to complexity. How ? here i had only two object overlapping with same data structure... what about if i have other object that are overlapping on something else? or i have sealed base class which i can't create a wrapper around?, in one method, my target is X and Y of object 1 and 2, but in another i'm targeting X and Z of object 1 and 3, sometime you got to define too many interfaces, and in such a case, we do not try to convey a protocol in our code, although our life is fulled with abstract classes and interfaces, but they had a propose. and do our lacks, we put our burden on them too. |
Beta Was this translation helpful? Give feedback.
-
For the situation where you have multiple types that have a similar structure, and you want to treat them in the same way, check out Exploration: Shapes and Extensions #164. The essence of that proposal is that you would declare a shape that described the structure your method needed - and anything that matched could be passed in. For things that were close, extension methods could be written to adapt the type (e.g. string has To illustrate, you could write something like this: public shape SEmailable
{
public string EmailAddress { get; }
}
public ValidationResult ValidateEmailAddress(SEmailable entity)
{
// elided
} And then any entity that had a public string property called |
Beta Was this translation helpful? Give feedback.
-
No, not treat them, they may or may not be treated the same way. and their core functionality and concept may or may not be the same.
Well that's what we learn because we are told to...
It was a little hard for me to understand the text, since i'm not native english talker, but as far as i see, it can overlap with our context here, but still that provide another hassles to remove another hassles that my appear in one block of code only, and you have to think what to add to that shared shape, what i'm looking at is C# just control our action, but not force us to add copeling, complexity, and more object to work with, etc... |
Beta Was this translation helpful? Give feedback.
-
Here's an example of how this can be easily solved with a switch expression (I'm still figuring out the nuances of switch expressions, so forgive me if my syntax is wrong, but you get the point): var email = switch (owner) {
case Agency a:
return a.ContactEmail;
case Person p:
return p.ContactEmail:
default:
throw new Exception("The world has come to an end");
};
if( !emailRegex.IsMatch(email)) {
return false;
} |
Beta Was this translation helpful? Give feedback.
-
Totally irrelevant... |
Beta Was this translation helpful? Give feedback.
-
@deadmann - I'd consider reflection. You could (in effect) create an object that "knows" how to access instances of both Agency and Person, then use that and pass the actual instance in along with the name of the property you want to access. Yes this doesn't have compile time checking but it can be made to operate very fast. If you designed this well you could end up with neat robust little library that does most of what you need. |
Beta Was this translation helpful? Give feedback.
-
Thank you for saying it in a fluent way, i couldn't get it right without your help, or being there explain it face to face as you put it into words... |
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.
-
I wish if it is possible to reduce duplicate code, and and generating less bug that happens naturally in big project due you edit something but you forget to do the same for another part, C# provide a way of multi-casting and force-casting:
I provide my propose with sample code from my own project:
Consider these 3 classes:
in here, if i want to write codes to check data within the Person and Agency, i have to write many duplicated validations, or I'm forced to introduce another level of inheritance:
My propose is to provide two new features:
( , )
operator:In above example, accessing to shared properties or Function with exactly same signature should be allowed, and un-shared properties should get filtered out:
So:
accessing
validateContact.FirstName
,LastName
,ContactTel1
,ContactTel2
,ContactEmail
,OwnerTitle
are allowed.But accessing
valiateContact.AgencyName
,AgencyWebsite
,RegistrationNumber
,NationalCode
, are denied.Also all properties from Owner and object, which are shared, are allowed too.
This should tell compiler to behind the scene rewrite these code separately for each object, reducing the duplicate code in user interface.
I propose as base of multi-casting, and on the same structure as that, another way of casting to be provided... Using
({ })
operator.Think of same classes as above, in a less or more complex structure, with no inheritance and such, you want to first write the structure, you do now know what the user may provide, but you want to force them to use specific structure if they are going to do these operation...
I think this may be fitting, if we say we want to do some operation only on
ContactTel
andContactEmail
, and we need some methods in which we know the signature of, we don't care about other stuff, yet we are not aware of the outer system, we force user to provide those fields...:We can do this:
Also we could provide extra operator for this matter, same as
nameof()
operator...if it be:
signatureof()
the following will be the result:The end.
Beta Was this translation helpful? Give feedback.
All reactions