Why not automatically infer constraints?

Why not automatically infer constraints?

Rate This
  • Comments 27

UPDATE: Whoops! I accidentally set a draft of this article to automatically publish on a day that I was away on vacation. The fact that it was (1) not purple and (2) introduced the topic and then stopped in mid-sentence were both clues that this was an unfinished edit. Sorry about that; I thought I had set it to *not* publish on Monday. I am not so good with these computer thingies apparently.

I spent the weekend not thinking about computers by lying beside a pool in Palm Springs, which I can definitively report is an awesome way to spend a rainy weekend in Seattle. I can also definitely report that Peter Frampton has lost almost all his hair and absolutely none of his talent; the man is amazing. If you like 1970's hard rock guitar solos, you've got just a couple more weeks to hear him perform all of Frampton Comes Alive.

Right, let's actually finish off that article then:

--------------

Suppose you have a generic base type with a constraint:

class Bravo<T> where T : IComparable<T> { ... }

If you make a generic derived class in the obvious way:

class Delta<U> : Bravo<U> { ... }

then the C# compiler gives you an error:

error CS0314: The type 'U' cannot be used as type parameter 'T' in the generic type or method 'Bravo<T>'. There is no boxing conversion or type parameter conversion from 'U' to 'System.IComparable<U>'.

Which seems reasonable; every construction of Bravo<T> is required to meet the constraints on T and we have no evidence whatsoever that the type supplied for U will meet those constraints.

But that's only one way of looking at the problem; another way of looking at it is that we do have evidence that the person who declared U expected that U meets the constraints of T, since they used it as T. Given that evidence one might reasonably then expect the compiler to simply silently place the same constraint upon U. U would then meet the constraint on T; any error then would not be on the declaration of Delta's base class, but rather, upon any code which constructs Delta<U> such that Bravo<T>'s constraints are violated.

I'm often asked why the compiler does not implement this feature or that feature, and of course the answer is always the same: because no one implemented it. Features start off as unimplemented and only become implemented when people spend effort implementing them: no effort, no feature. This is an unsatisfying answer of course, because usually the person asking the question has made the assumption that the feature is so obviously good that we need to have had a reason to not implement it. I assure you that no, we actually don't need a reason to not implement any feature no matter how obviously good. But that said, it might be interesting to consider what sort of pros and cons we'd consider if asked to implement the "silently put inferred constraints on class type parameters" feature.

As C# has evolved, clearly more and more features involve the compiler silently making inferences on your behalf: method group conversions, method type inference, implicitly typed locals, implicitly typed arrays, implicitly typed lambdas, and so on, all involve a great deal of inference work by the compiler. You would think we could do the same thing for generic type constraints. However, the problem is not as easy as it looks. The easy case mentioned above is, well, easy. Things quickly get complicated:

class Echo<W, X> where W : IComparable<W> where X : IComparable<X> { }
class Foxtrot<Y> : Echo<Y, Y> { }

The supposition here is now that Y must be implicitly constrained to be both IComparable<Y> and... IComparable<Y>.  OK, that seems reasonable. But what if we then change that to:

class Echo<W, X> where W : Foo where X : Bar { }
class Foxtrot<Y> : Echo<Y, Y> { }

Suppose Foo and Bar are class types with no relationship between them. Now there is no possible type argument for Y that meets the inferred constraint. Is the compiler now required to tell you that fact? How smart does it have to be about that?

Let's make this a bit more complicated. I'll just make something up off the top of my head:

class Golf<T> where T : Hotel<T> {}
class Hotel<U> : Golf<Indigo<U>> where U : IJuliet<Hotel<U>> {}
class Indigo<V> : Hotel<Golf<V>> where V : IKilo<V> {}
interface IJuliet<W> {}
interface IKilo<X>

What are the constraints that we have to infer? Well for T to be a Hotel<T>, T has got to be an IJuliet<Hotel<T>>, so we should add that constraint. But in order to be an IJuliet<Hotel<T>>, T has got to meet the constraints on Hotel<T>... oh, wait, we already added that constraint. Looks like our constraint adder is going to have to be resistant to cycles. But wait, do we have *all* the constraints on Hotel<T>? So far we have only evaluated the *stated* constraints. U in Hotel<U> not only has to be an IJuliet<Hotel<U>>, it also has to be a legal Indigo<U>, which means it needs to be an IKilo<U>, which means we should add a constraint to T that T be IKilo<T>.

And so on. I'm not going to go through a full analysis of this thing. The point is, the analysis is complicated, the analysis may go into infinite loops very easily, and it is not at all clear when you know that you've worked out the full set of type constraints when types refer to each other in arbitrary ways. (And we haven't even considered what happens if the interfaces are covariant or contravariant!) I don't like adding inference algorithms to the compiler that require cycle detection and do potentially unbounded amounts of work. The possibility of getting the algorithm wrong is very real. Even if we get the algorithm right, the odds that the implementation will be buggy is very high. If we add this feature then the compiler needs to be able to get the right answer all the time, and to give a sensible error message that helps the user if there is no right answer. Both problems seem very difficult.

Moreover, why are we stopping with base types? It seems like if we're going to do the feature of inferring constraints, then we ought to consume information about the entire class, not just the base types:

class Mike<T> where T : November {}
class Oscar<V> { Mike<V> bv; }

Should we deduce that V must be November because Oscar<V> has a field of type Mike<V>? I don't see why the base types are any more special than the field types.

What if Oscar<V> had used Mike<V> as a return type of a method? Or a parameter type? Or as the type of a local variable? Where does it stop?

Basically, the feature is too much pain for too little gain. When you construct a generic, you are the one required to supply a proof to the compiler that you have satisfied the constraints. C# is not a "figure out what the developer meant to say and say it for them" language; it's a "tell the developer when they have supplied too little information" language.

  • I couldn't find time to read stackoverflow.com/.../8606679 very carefully, and I don't claim that I understand all the implications of the proposal but since you asked, I'm all for it.

    Anything that makes the compiler smarter on type inference is welcome. The introduction of "var" has made coding easier and also made the code more readable. And anything that makes the type system more powerful (as in covariant&contravariant contraints in C# 4.0) is welcome.

    I'd like the type system be more powerful. I wish we had multiple dispatch. I wish we had more powerful constraints. I wish we had overloading based on return types of methods. Hell, I wish we had something like Duck Typing, i.e. if I call x.Foo(), then infer that x is implementing IFoo(), and every class with a Foo() method automatically implements IFoo(). I'd like C# code to be as flexible as a dynamic typed language like Smalltalk, the compiler to be more clever on type inference than ML compilers, and still be safe, and perform as few run-time type-checks as logically possible.

    "To let the compiler infer too much is dangerous" many people say. I disagree. Let the compiler be smart but also very transparent. No implicit type casts, or at least make it (optionally) visible on the screen. No implicit boxing, or at least make it (optionally) visible on the screen. If it produces C# code as an intermediate step when it transforms LINQ expressions, let me be able to see that intermediate step on the screen. If it makes an inference about a generic type T, let me be able to see that inference. Then we get the best of all worlds. A clean, short, readable code, a powerful type system, a smart compiler, and ability to see what is wrong when something is wrong.

    By the way, Eric, I enjoy and learn from your blog and answers at stackoverflow very much, I hope I don't sound like whining or bickering; I'm not an expert and I may not be correct in terminology or sensible in my wishes but anyway this is what they are.

    Your proposal to make inferences on type constraints is not as large as my overgrown and unrealistic wishes but it is a step in the right direction.

  • You are being WAY to literal. I used to be just like you. It took me years to get my head out of logic text book and do the obvious translations in my head, "Why doesn't the compiler implement X". Come on! You're clearly smart enough to realize that they did not literally mean the question they asked. Why not answer the question they really wanted an answer to instead? Everyone, including you, would be much happier. Just say, "We considered the pros and cons of X and it did not meet the bar to be implemented." Or "That's a great idea. We'll have to consider all the pros and cons...." Then you can have a nice and polite conversation about those pros and cons....instead of what you appear to do now, which is be an ___ ____.

  • @David. Be nice.

    As far as I'm concerned Eric can be as literal as he damn well pleases, I am simply grateful that he shares his experiences with us, if he wants he can type each post shifted one key to the right ;olr yjos, and I'd still read them.

    Now stop being an sdd jp;r, shut up, and leave the nice man alone.

  • Am I the only one thinking that this post is actually unfinished draft that was accidentally submitted? And judging by Eric's absense in the comments he might actually be not aware of this.

  • Eric, the assumption that your user's tend to make (that you should have had a good reason NOT to implement feature "x") is very common in all development. I find it amusing that software developers make this same assumption, yet when we're writing software we tend to get angry when OUR users make that assumption. It's rather ironic if you think about it.

  • @Juozas No, you're not the only one. It does seem like the post is cut off at the end.

  • Base<T> where T: class // in someone else's dll

    D<T> : Base<T> { // infers constrains

          M(T t) {

                if (t==null) {.   //.  Whoops!!  If base changes it's constraints, this might not work

  • @Jon Skeet : "It looks like you have no clue what you're doing - would you like to ask on Stack Overflow?"

    Eric, this is the best feature request I have seen in the blog comments so far! Please implement in the next version of C# compiler!

  • @David: "I'll look into that feature" is not an answer to "Why isn't this feature implemented?"

    @Konstantin: That could probably be a plug-in... get coding :)

  • The examples you gave definitely helped me understand why too much inference of type constraint is a bad idea. I think your last paragraph could be more detailed about it.

    This problem is, the inference feature would conflict with other inferences features, and then error reporting would become bad, because when writing error messages, the compiler wouldn't know where the code is supposed to be wrong. In C# with VS, the quality of the error messages are an important and polished part of the programming experience, so the best tradeoff is to not infer type constraints.

    Everything cannot be infered anyway, sometimes you have to have the programmer state some things explicitly. And for type constraints, the class declaration is a very good place to do so.

  • Hmm, two things come to mind.

    First, these type constraints are veeery similar to what you find in languages with type classes, such as Haskell and Mercury, and they have solved the inference problem.  I don't think it's as big a can'o'worms as you imagine (of course, I may have missed something: OO type systems tend to be full of strange corners).

    Second, since you have to *check* these constraints in the compiler, haven't you already done much of the inference work here?

  • There is a middle ground between having the compiler infer something and requiring you to state your requirements explicitly, and that middle ground is named clippy ;)

    Actually what I mean is, if a compiler or "lint" tool can find a change that will fix a problem -- whether it be adding a type constraint, adding a cast, adding a semicolon, or changing "var Z = new..." to "Dictionary<string, object> Z = new..." because Z is a field, then the error message in the IDE should have a little "autocorrect" button beside it.

    The advantages of this approach compared to "inferring what you meant" is (A) less work for Eric Lippert -- the compiler doesn't have to handle all cases, only the most common ones, (B) the inferred information becomes part of the source code so the developer can see it, (C) any inference bugs are never serious--the developer can correct the compiler's guess if it guesses wrong.

Page 2 of 2 (27 items) 12