FR: Make Delegates easier to use in the IDE

Over the years I have I tended to make quixotic feature requests that i know would be useful, but would likely never see the light of day… Well here is another one:

<https://xojo.com/issue/58602>

Delegates are nice feature, but while I can often remember the name of a delegate I created ,I often forget the details of the signature…

It would be nice if the IDE could help with that just as it does for interfaces as they are similar in principle…

It would be nice to have a “Implement Delegate" menu option that would bring up a dialog with a list of all delegates that are within the scope of the item (if any are available) from which we could select one, name the method and the method created with right parameters and return type and puts us into it’s code editor.

While delegates may be consider an “advanced” feature, this would make them easier to use and encourage their use more…. kind of make them a bit less ‘advanced’ in a sense.

I think it would also encourage those providing 3rd party code to use delegates in their APIs as it becomes easier for their customers to use them as well.

If you are using r2 or r3, you can right-click on a class in the code editor and you’ll find delegates and events under the Add Method menu item. It’ll even add the object as the first parameter for events!

Can you explain that in more detail? Perhaps with a screenshot? I haven’t a clue what you mean.

In the code editor, if you have the code

Var t as new Timer

Right-click on Timer and select Add Method > From Event > Action.

I never ever would have noticed that using Xojo. You need to have better docs for your new features. Really. That feature is really cool.

[quote=467245:@Greg O’Lone]In the code editor, if you have the code

Var t as new Timer

Right-click on Timer and select Add Method > From Event > Action.[/quote]

I am talking about delegates that are Used by Invoke, not just with AddHandler!

-Karen

[quote=467258:@Karen Atkocius]I am talking about delegates that are Used by Invoke, not just with AddHandler!

-Karen[/quote]
Right. For classes which contain delegates, there will also be a “From Delegate” option.

I think you don’t understand what I mean…

As I said I am talking about making it easier to use Delegates more generally than just for Events or in classes as general function pointers.

I want to use it as function pointer that is passed to a method as parameter… Let me given you an example

I have a method to be used in a wide variety of situations but who’s behavior I sometimes needs to be altered to handle special cases . if the method was in a class one could use an event for that …

BUT besides the significant limitation of needing a class, events are not obviously specific to a specific method, but only to the class.

As such they don’t make an obvious easily discoverable API for the method (who reads docs? :wink: )

However in this case an event was not even an option as the method is not on a class but in a module. The method is on an extension method on an Enum so has to live in a module.

More specifically in this case it it is concerting one type of enum based on some parameters and returns a different type of enum

Passing in in delegate object has the advantage of being both specific to the method and having that shown on teh information line in the IDE…

Basically I am talking about having a delegate definition in a module such as

Public Function ConversionDelegate(Param1 as SomeType ) as EnumType

The Extension method is something like:

Public Function ToEnum2(Extends aValue as Enum1, aParam As AnyType, aDelegate As ConversionDelegate = NIL ) As Enum2

All the above can be done already. But once the delegate is created in the IDE, creating methods that implement that signature should be able to be done in a way analogous to adding interface methods.

That is the part that is missing to make using Delegates as function pointers that can be passed a parameters to methods more convienient, and so used more

Does that make sense?

  • karen

Got it.

The trick is that the best way to know what it “in scope” for a particular item at any given time, we really need to ask the compiler… and we don’t currently have a way to do that. Otherwise we’re just guessing and like autocomplete, we’d sometimes get it very wrong.