Comparing Delegates

Consider the following code:

dim fooDelegate as Class1.Foo
dim barDelegate as Class1.Bar

fooDelegate=new Class1.Foo(AddressOf NoParamMethod)

barDelegate=new Class1.Bar(AddressOf NoParamMethod)

if fooDelegate isa Class1.Foo then
  break
end

if fooDelegate isa Class1.Bar then
  break
end

Foo and Bar are Delegates in Class1, both with no parameters. NoParamMethod is a method on Class1 with no parameters. When this code is run, both breaks are hit.

If I change the method signatures of either delegate; for example, if I add an integer parameter to the Foo delegate; then only the first break is hit. This suggests that the IsA operator is considering the method signature.

This strikes me as odd behavior. Delegates sure look and behave like single-method class interfaces, but class interfaces never compare like this. Shouldn’t Delegates with the same signature still be distinct? As it stands, there’s no way to distinguish between two Delegate objects with identical signatures.

In my use case, I was thinking about having multiple Delegates in a dictionary and using their “class” via IsA to determine how to handle them (for example, one Delegate might have an integer parameter that indicates width, while another might have an integer parameter that indicates height).

This also breaks at both places:

dim fooDelegate as Class1.Foo
dim barDelegate as Class1.Bar

fooDelegate=new Class1.Foo(AddressOf NoParamMethod)

barDelegate=new Class1.Bar(AddressOf NoParamMethod2)

if fooDelegate isa Class1.Foo then
  break
end

if fooDelegate isa Class1.Bar then
  break
end

Could you share a project?

Here’s the link to the documentation for delegates so you can review what Xojo says they are and how to use them: Advanced language features — Xojo documentation

Thanks, I did read the documentation. :slight_smile: They don’t say anything about comparisons.

The method signature is the only basis for comparison. Delegates need not even refer to a method defined by a class (i.e., module methods).

1 Like

This is a nonsensical statement.

1 Like

Well, the compiler disagrees: that code compiled and doesn’t crash. :wink: It produces consistent results, even though I find them confusing.

They are confusing because you’re using it wrong. A Delegate is intended to completely decouple from the method being called. As long as the parameters match, it could point to any method at all. There is no way to get the identity of that method; it isn’t intended to.

I’m not trying to get the identity of the method the Delegate points to. I’m trying to get at which delegate definition the Delegate is based on - Foo or Bar.

I don’t see any way to do this at present, which seems odd.

If you use MBS Xojo Plugins, check out this helper functions we made for ourselves:

A delegate is kind of a class and so IsA can work there, too.

1 Like

If Foo and Bar have the same method signature, then they are indistinguishable to the delegate.

1 Like

Apparently so. Well, there ought to be a way. :wink:

Submit a feature request.