2

I want to do an application that pareses text. So far, I have a class called Result, that holds the value and type each part of an equation.

public enum ResultType
{
    Int32,
    Double,
    Boolean,
    Color,
    DateTime,
    String,
    Undefined,
    Void
}

public class Result
{
    public object Value { get; set; }
    public ResultType Type { get; set; }
}

Possible Result's could be:

 5 : Int32
 true : Boolean 
 DADACC : Color 
 "Hello World!" : String 
 10.0 : Double 
 13/11/1986 : DateTime

Now I want to sum/divide/pow/... two Results but I really don´t want to do all the work. In C#, you can mix them all together and get an answer.

var value = "Hello" + 2.0 + 4 + DateTime.Today; (value = "Hello2413/09/2011 12:00:00 a.m.")

Is there an easy way to handle this? Or do I have to figure out all combos by myself? I´m thinking about something like:

var Operator = "+"; // or "-","*","/","^","%"
var sum = DoTheCSharpOperation(Operator, ResultA.Value, ResultB.Value)
var sumResult = new Result(sum);
Marco Bruggmann
  • 605
  • 2
  • 12
  • 23
  • 8
    What would a Boolean to the power of a Color be? – Coeffect Sep 13 '11 at 22:50
  • 1
    It sounds like what you really want is a programming language. What problem are you really trying to solve with this? – StriplingWarrior Sep 13 '11 at 22:55
  • 2
    It all works in C# because *the compiler figures out which operators apply (if any) and generates the correct IL*. That step is non-trivial. – dlev Sep 13 '11 at 22:55
  • Boolean to the power of a Color would be an exception of "cant-do-that". I want to use C#'s brain to figure out what the sum of two objects is(each of those have a type and the compiler knows what the type is) – Marco Bruggmann Sep 13 '11 at 22:59
  • I think you should reverse your example of input to output and make the output match the input values ...I think ...'cause I *think* I understand your question ... – IAbstract Sep 13 '11 at 23:03
  • This is not a particularly good use of an OOP language. The idea of OOP is to have things that are strongly typed in such a way as to prevent people from compiling statements that are non-sensicle. "Guess and check" generic operations is not included as one of C#'s best practices, unfortunately. Increasing compiler errors, while simultaneously decreasing run-time errors will make your programming career much easier. – EtherDragon Sep 13 '11 at 23:07
  • @EtherDragon I know it's not the best practice, but for what I need it, it good enough. – Marco Bruggmann Sep 13 '11 at 23:52
  • @Mannimarco, do you use the "+" symbol? thats a text addition. There are statements that make sense and other that does not. I just didn't wanted to write as much. Thanks :) – Marco Bruggmann Sep 13 '11 at 23:52

1 Answers1

9

This sounds to me like a perfect application for the "dynamic" keyword:

using System;
using System.Diagnostics;

namespace ConsoleApplication33 {
  public static class Program {
    private static void Main() {
      var result1=DoTheCSharpOperation(Operator.Plus, 1.2, 2.4);
      var result2=DoTheCSharpOperation(Operator.Plus, "Hello", 2.4);
      var result3=DoTheCSharpOperation(Operator.Minus, 5, 2); 

      Debug.WriteLine(result1); //a double with value 3.6
      Debug.WriteLine(result2); //a string with value "Hello2.4"
      Debug.WriteLine(result3); //an int with value 3
    }

    public enum Operator {
      Plus,
      Minus
    }

    public static object DoTheCSharpOperation(Operator op, dynamic a, dynamic b) {
      switch(op) {
        case Operator.Plus:
          return a+b;
        case Operator.Minus:
          return a-b;
        default:
          throw new Exception("unknown operator "+op);
      }
    }
  }
}
Corey Kosak
  • 2,615
  • 17
  • 13
  • Could you please explain why that is working? It works, but how?:D – Marco Bruggmann Sep 13 '11 at 23:22
  • 1
    I'd love to explain if I could but the topic is big and complicated and I probably wouldn't do it justice. Basically it is part of the "Dynamic Language Runtime", meant to support dynamic languages like Ruby and Python. The basic idea is that the compiler writes code that records what you were trying to do with the dynamic type, and then at run time the runtime system figures out how to do it. By "what you were trying to do" I mean not only using operators, as in our example above but also function calls, accessing properties, explicit casts or conversions, etc. – Corey Kosak Sep 14 '11 at 01:13
  • 2
    It's pretty sophisticated what it can do--basically making the decisions that the compiler would make but at runtime. If used in an undisciplined manner, it could make your program a maintenance nightmare, but in the right situations it is pretty helpful. This web page might be a good place to start reading: http://www.codeproject.com/KB/cs/TheDynamicKeyword.aspx – Corey Kosak Sep 14 '11 at 01:15
  • 1
    Here's another cute little program that demonstrates how it could be used for double-dispatch: using System.Diagnostics; namespace N { class Program { static void Main() { dynamic a=new A(); dynamic b=new B(); dynamic x=new X(); dynamic y=new Y(); a.Foo(x); a.Foo(y); b.Foo(x); b.Foo(y); x.Foo(a); } } class A { public void Foo(X x) { Debug.Print("in A.Foo(X)"); } public void Foo(Y y) { Debug.Print("in A.Foo(Y)"); } } class B { public void Foo(X x) { Debug.Print("in B.Foo(X)"); } public void Foo(Y y) { Debug.Print("in B.Foo(Y)"); } } class X {} class Y {} } – Corey Kosak Sep 14 '11 at 01:26