This forum has been archived. Please start a new discussion on GitHub.

How to define uncertain variable type

In c#,there is a variable type 'var',how to define it in ice?


  • matthew
    matthew NL, Canada
    Slice has no such construct. What is your use case?
  • I want to use it in a interactive system.the server can give me a may be int/float/string etc.
    how i achive this object use ice?
  • The best option is almost certainly to define separate operations:
    interface Example {
        int getIntValue(/*...*/);
        string getStringValue(/*...*/);
        // ...

    You can use Slice classes to get the effect of a union. For example:
    enum ValType { IntValue, StringValue /* ... */ };
    class ValueBase {
        ValType type;
    class IntegerValue extends ValueBase {
        int val;
    class StringValue extends ValueBase {
        string val;
    // etc...
    interface Example {
        ValueBase getValue(/*...*/);

    The receiver of the ValueBase looks at the enum to decide how to down-cast the instance.

    Which approach is better for you depends on your application. Be warned though that unions are often unnecessary and can be an indication of a half-baked design, so I'd carefully review the design before committing to this approach. (Unions tend to undermine the type system because they delay the decision as to what type is actually exchanged until run-time. Sometimes, that can be just what is needed. However, I quite often see unions that, with a bit of thought, could have been avoided, resulting in an easier-to-use system with stronger type safety.)


  • bernard
    bernard Jupiter, FL
    Also, if both your client and server are C# or .NET, you could simply pass this parameter "as-is" (provided it's serializable), without mapping it to a Slice type.


    Best regards,
  • thank you for your advice!