Math operations on arbitrary objects in C# -


I am implementing an interpreter for a toy language in C #, and to math in that language, I Like to implement a function like this:

  Add a public static object (object A, object B) {// back every A and B / / int, double, Or a number of other numerical types}  

I have a very silly and bad implementation of this function Can assume that A is put) and a group using the B-type (operator, but my instinct is that better way.

Do you think there will be a good implementation of this function?

If you want to apply your rules in addition to this, then welcome to my world there is no magic road which avoids many types of checks and switches. In addition to two arbitrary types, there are literally hundreds of potential cases and you have to see them all.

The way we handle this complexity in C #, The additional operators define the small subsets of type: int, uit, long, ulong, decimal, double, float, all emam, all reps, strings, and all the blank variants of those types of values. (Enums then have their underlying type form For example, when you add a short word, then we make the problem easier by saying that we both have two special cases , And then add two ints to solve the problem. It cuts down the amount of code widely to write but I believe, there are thousands of lines of binary operators overloaded resolution algorithm code in C #. This is not an easy problem.

If the purpose of your toy language is a dynamic language with its own rules, then consider using the DLR mechanism to implement other functions like implementing IDmaticMetaabageProver and implementing other functions like math and implementation calling. can do. / P>


Comments

Popular posts from this blog

c# - sqlDecimal to decimal clr stored procedure Unable to cast object of type 'System.Data.SqlTypes.SqlDecimal' to type 'System.IConvertible' -

Calling GetGUIThreadInfo from Outlook VBA -

Obfuscating Python code? -