Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is taking advantage of optional parameter edge cases to force implementations of ToString via an interface abuse of the language?

I have an interface IKey which I want to have a method which will return the key as a string. We looked at having a method like this:

String GetAsString();

which would return the string representation, but would have liked to be able to declare ToString() again in the interface to force implementers to implement it, but it doesn't force them to as they have an implementation inherited from Object. This was suggested:

public interface IKey
{
    string ToString(string dummyParameter=null);
}

this forces an implementation of the method in any implementing class, but due to the way that optional parameters work callers do not need to provide a value for this, and you ensure that any calls to the ToString() method on objects which are either cast as the interface IKey or the implementing class will always call the class implementation and not the Object implementation.

In the implementations we can just ignore the dummyParameter and return what we want, safe in the knowledge that calling ToString() will always actually call ToString(null).

Now this feels wrong all over to me, but at the same time it does have something quite nice about it. It is almost exactly the same as having a method GetAsString() as this could only be called on the IKey interface and derived classes except that it looks like the more natural ToString() method that we want to use and that we are able to force the implementing of in the child class.

Having said that the dummy parameter which is not used feels wrong.

So is this horrendous? Or great?

And is this question appropriate for SO or should it be on Programmers?

Examples

public class Key :IKey 
    { 
        public string ToString(string abc = null) 
        { 
            return "100"; 
        } 
    }

Key key = new Key ();
Trace.WriteLine (key.ToString());
Trace.WriteLine (key.ToString(null));
Trace.WriteLine (key.ToString("ac"));
Trace.WriteLine (((object)key).ToString());

output:

100
100
100
Blah.Tests.Key
like image 699
Sam Holder Avatar asked Oct 24 '11 20:10

Sam Holder


2 Answers

It sounds like you're using an interface where you should be using an abstract class. The class below explicitly requires descendants to have implemented ToString.

abstract class X
{
    public abstract override string ToString();
}
like image 114
Austin Salonen Avatar answered Nov 03 '22 00:11

Austin Salonen


From my point of view such ToString() method in a custom interface slightly messed up things because a custom interface exposed a method with standard and well known name ToString().

I prefer something more straightforward and obvious like:

string KeyText { get; }

OR method

string ConvertKeyToString();
like image 44
sll Avatar answered Nov 03 '22 02:11

sll