Generics T constraint

Generics T constraint
TMyClass = class
private
  FReference: T;
public
   property Reference:T read FReference write FReference;
end;

T needs to be either a class or a record but you can't combine those constraints.  The obvious workaround is 
TMyClassC = class(TMyClass);
TMyClassR = class(TMyClass);

That is a bit annoying :P

Comments

  1. Better to use a runtime test in my view. Pretty unusual to want to constraint to be either class or non-nullable value type. Your property declaration is not valid.

    ReplyDelete
  2. We need to make a big list of all the things that should change in generics (and constraints) and put them in a single big QP.

    ReplyDelete
  3. David Heffernan I guess you can say the declaration is "meta code" and it is left to the reader to imagine the field/setter/getter.

    ReplyDelete
  4. The reason for desiring the combination of these two, is that both have fields and properties which can be RTTI enumerated the same way.

    ReplyDelete
  5. So use a field and this make the code even short, and also valid. You post a lot of code of this nature that does not compile and so forces anyone trying to help you to do extra work. Not an issue here, but it has been on other times.

    ReplyDelete
  6. David Millington There should probably one "mother QP" which link the issues - but it usually is a good idea to separate requests/reports.

    ReplyDelete
  7. David Heffernan The point of constraints are type safety to catch mistakes at compile time. I would prefer not to give that up for run time checks - although those are a fallback method.

    ReplyDelete
  8. David Heffernan Point taken on compilable code example, although Ctrl+Shift+C would have completed it into valid code.

    ReplyDelete
  9. I understand what constraints are for, but I'm not sure that what you have produced is better. It just adds confusion and clutter to my mind. I think the downsides outweigh the gains.

    I'm curious though, why do you want to restrict to classes or non-nullable value types? That seems unusual.

    ReplyDelete
  10. Because the class will enumerate the fields and properties of type T, looking for specific attributes, and it would be undesireable to have T's which are not classes or records.

    ReplyDelete
  11. I'm still hanging out for typesafe "duck-typing" in Delphi. Ie, if a type implements an interface, even by accident (not specifying that interface), allow it to be treated as an instance of that interface. That would be useful in many areas, not just generics.

    ReplyDelete
  12. Like strings, or interfaces, or anonymous methods? It sounds odd that an array of characters would be valid, but a string would not. Or an integer would be valid but a string would not. Or a record containing a single string would be valid, but a string would not.

    ReplyDelete
  13. Perhaps the a new constraint could actually be structure to separate it from simple types.
    TMyClass

    ReplyDelete
  14. And there we have it. The record constraint does not restrict you to compound structures declared with the record keyword. Simple types meet the record constraint. So Integer and double both meet the record constraint.

    ReplyDelete
  15. David Heffernan I did not know that.  Even more reason for a structure or compound constraint.

    ReplyDelete
  16. This is why I tend to ask why rather than give the naive answer. Doing so often reveals something of use. You recently suggested that I should not do that. I think it's more useful to poke and prod at the reasons behind a question or post.

    ReplyDelete
  17. But all the same, I cannot see why you would be interested in providing a facility that only worked for compound types. That seems needlessly limiting. Aren't you going to end up making the code less capable than it needs to be?

    ReplyDelete
  18. I am trying to make it less complex by eliminating capabilities that are not needed, and hence reducing the test scopes.

    I don't mind your questions when they are constructive.

    ReplyDelete
  19. My children ask me stupid questions sometimes. It's better that they did that than never to ask at all.

    ReplyDelete
  20. There are no stupid questions, IMO, but - to quote the despair.com T-shirt - " - there are a lot of inquisitive idiots." 😇
    http://despair.com/products/cluelessness

    ReplyDelete

Post a Comment