Delen via


Why not automatically infer constraints?

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.

Comments

  • Anonymous
    March 09, 2012
    The comment has been removed

  • Anonymous
    March 09, 2012
    I'm not against inferences generally. However, in this case I think that the non-feature is actually better, because it places a potential error closer to the root cause. If you infer the constraint, then the user can get into trouble if they fail to provide a satisfactory type when using Delta<U>. The error will be correct, but if the user goes and looks at the declaration of Delta<U>, there won't be anything there to tell them why the constraint exists. They have to go look at Bravo<T>. In this small example, that's not hard to do. But what about a more complicated scenario in which there are several inherited/implemented types? Then the user has to go look at each one to see if any have a constraint from which the Delta<U> constraint is being inferred. The way it is now, if you fail to declare the constraint, you get a specific, (somewhat) clear message explaining that the constraint hasn't been met, and which constraint for that matter.

  • Anonymous
    March 09, 2012
    I feel like the second of the last paragraph could be the inscription on a very interesting book...

  • Anonymous
    March 09, 2012
    "As C# has evolved, clearly more and more features involve the compiler silently making inferences" Presumably this means that in C# 10, if the compiler infers that you're a truly terrible coder, it will add code to make sure it can never do any harm in a production environment? Or perhaps Visual Studio should have a Clippy-like helper: "It looks like you have no clue what you're doing - would you like to ask on Stack Overflow?" On a more serious note... has your computer run out of purple pixels? I know you've been using a lot of them, but even so...

  • Anonymous
    March 09, 2012
    @Allan, I think there is something much more important missing in the article: purple!

  • Anonymous
    March 09, 2012
    Ignore my previous comment.

  • Anonymous
    March 09, 2012
    To be fair, I'd much rather see constructor inheritance than automatic constraint inferring :)

  • Anonymous
    March 09, 2012
    Ditto @Michael Stum!  Constructor inheritance +1 vote.

  • Anonymous
    March 09, 2012
    "The answer to 'Why doesn’t this feature exist?' is usually 'By default features don’t exist. Somebody has to implement them.' It’s not like every feature you can think of comes out of your brain fully tested and implemented, and then some PM somewhere files a bug to have your feature removed. Features start out nonexistent and somebody has to make them happen." Raymond Chen 23-JUN-2005

  • Anonymous
    March 09, 2012
    The comment has been removed

  • Anonymous
    March 09, 2012
    The comment has been removed

  • Anonymous
    March 09, 2012
    Hi Erick, in a previous post "blogs.msdn.com/.../the-future-of-c-part-five.aspx" you commented "calling base methods from anonymous methods: Yes, we fixed that." I think you fixed it for C#4... what happened for C#3.5??? did you do a patch???

  • Anonymous
    March 09, 2012
    Sorry for asking about that feature in a post for another feature... I just thought you wouldn't read 4-years-ago posts anymore

  • Anonymous
    March 09, 2012
    When I read this blog post, I remembered having this discussion on Stackoverflow(stackoverflow.com/.../997973) What happened to the arguments you mentioned? Personally, I think automatically inferring constraints, would make the code harder to understand. But the IDE could offer a smart-tag to insert the constraints automatically instead of having to copy them manually from a base class. Maybe even support it as a refactor option (Add constraint to derived classes). The same could be done for constructor inheritance!

  • Anonymous
    March 09, 2012
    I take the opposite view - I don't want ANY defaults for ANY programming element. Ever. Main reason: If you did not intend the Default - and the compiler "silently implements it" then somewhere down stream a defect will occur. Good example - default modifiers in C# Second reason: Too many languages, too many defaults to remember Third reason: When learning a language "silent compiler actions" are just plain confusing

  • Anonymous
    March 10, 2012
    The comment has been removed

  • Anonymous
    March 11, 2012
    The comment has been removed

  • Anonymous
    March 11, 2012
    The comment has been removed

  • Anonymous
    March 11, 2012
    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.

  • Anonymous
    March 11, 2012
    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.

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

  • Anonymous
    March 12, 2012
    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

  • Anonymous
    March 12, 2012
    The comment has been removed

  • Anonymous
    March 12, 2012
    @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 :)

  • Anonymous
    March 14, 2012
    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.

  • Anonymous
    March 20, 2012
    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?

  • Anonymous
    April 03, 2012
    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.