Double duck typing

I ran into a situation when using some SOAP services that is a bit annoying. There are two services, both of which use a lot of the same help classes as part of their methods. Each of these classes might have some properties that point to instances of other classes. I wrote some functions that need to be able to take those same classes but they can’t because of the nested helper classes.

This is probably confusing, but here’s an abstract example:

  Foo1 = public class
  public
    property Prop: String := '1';
  end;

  Class1 = public class
  public
    property Obj: Foo1;
  end;


  Foo2 = public class
  public
    property Prop: String := '2';
  end;

  Class2 = public class
  public
    property Obj: Foo2;
  end;

So I have a function that I want to have take either a Class1 or a Class2, since they’re really the same class (from a human’s point of view). I tried duck typing, but the problem is that it seems duck typing only works at the most surface level. It got all ducked up when I had to confront their being a Foo1 and a Foo2.

I tried

  IFoo = interface
    property Prop: String read write;
  end;

  IClass = interface
    property Obj: IFoo read write;
  end;

but that was no good. When I tried to do

  var c := new Class1;
  var ci: IClass := duck<IClass>(c);

it just did not like it at all, since the classes do NOT use IFoo. I’d need the compiler to have a deeper understanding of ducks.

Anyway, I think I’m just going to have to maul this generated service code myself, as I can’t do anything to fix it upstream. I just thought I’d throw it out there for comments.

Thanks, logged as bugs://66059: Double duck typing

Sounds like a reasonable request. We’re pretty much locked down for february, feature wise, but it’s on the list for consideration after feb. Thanks!

Interesting! I didn’t think it would really be something that would be feasible. How deep do you think it could go in the chain? Because inevitably, I’ll find that Foo actually has an instance of a Bar class in both, you know. :smiley:

heh. So far it’s more of a research issue. These things usually become visible once I start trying them.

this can now be done with extension methods & duck typing.

1 Like

bugs://66059 got closed with status fixed.