How to Reference a Procedure

It was pointed out in the comments and on G+ that this covers type aliases, but not procedure aliases. Instead it shows how to have a variable that references a procedure and then allows you to call it by this new variable. While I used alias as meaning “reference by a different name” it does in fact have a specific definition in this context. Sorry if there was any confusion or undue frustration at imprecise terminology.

I just answered a question about how to alias a procedure. I thought it was interesting enough to share here.

It is easy enough to alias a type . . .

  my_integer = integer;

then we can just use my_interger in place of integer as a type. But what about a procedure (or function for that matter)?

There are two different ways, depending on if the procedure is a member of a class. For straight up procedural procedures and functions it looks a little something like this:

procedure HelloWorld; // declare our procedure

  my_proc: Procedure; // declare our alias

  my_proc := HelloWorld; // assign the alias
  // ...

  if assigned(my_proc) then // verify the reference
    my_proc; // call the alias

This is pretty straight forward. We just create an alias reference variable of the right type, and assign it to the procedure we want to alias reference. If you call an alias reference variable that is unassigned you will get a null reference access violation.

You can also streamline it a little like this

var // or as const
  my_proc: procedure = HelloWorld;

Then you know it is assigned. I guess this would be useful if you want to alias reference a procedure declared in a different unit.

This works the same for functions or procedures with parameters.

procedure Hello(name: String);
  ShowMessage('Hello ' + name);

function Nine: integer;
  Result := 9;

  argumentative: procedure(s: string) = Hello;
  number: function: integer = Nine;

Notice that the name of the argument doesn’t have to match, but the number, order and types do. If they don’t then you will get the error

E2009 Incompatible types: ‘Parameter lists differ’

Now what if you are dealing with procedures or functions that are members of an object? If you try to assign them to the above types you will get the error

E2009 Incompatible types: ‘regular procedure and method pointer’

And that is because members of an object are method pointers. Fear not, you can handle them with just a slightly different type declaration:

  TMethod = procedure of object;
  TFunc = function: integer of object;
  TNotifyEvent = procedure(Sender: TObject) of object;

In this case they are declared as types with “of object” added to the end. This indicates that they are procedures of an object. AKA members or method pointers.

You can read more about procedural types and method pointers in the DocWiki.

Why would you want to do any of this? First of all, the method pointer is how the VCL & FMX handles dependency injection through event handlers. This is also really useful when combined with anonymous methods and the parallel programming library.

Posted in Source Code | 6 Comments

Podcast Episode 60 – Victory Fernandes

In this episode I talk with Embarcadero MVP Victory Fernandes from Salvador, Brazil. While Victory is from Brazil, he also speaks fluent English.

Victory Fernandes

Victory talks about:

Most all of Victory’s projects are powered by Delphi and InterBase with emphasis on embedded and real-time application development.

Posted in Audio podCast, podcast | 1 Comment

REST Client Video Challenge

Yesterday, I mentioned I built a REST client and deployed it to iOS and Android inside 5 minutes. Someone asked me to make a video actually showing how it was done. So here it is showing a simple REST client built and deployed to iOS 9.1 and Android 6.0 Marshmallow.

Since I am using SpitCast, and it doesn’t offer an encrypted endpoint for its REST service, I had to modify the Info.plist to allow that on iOS 9.

Could I do it faster without the video recording and explanation? Yes, but I think you get the idea. Delphi makes building multiplatform REST clients really, really easy.

I’m curious to see someone create a client app using other general purpose development tools to connect to a REST service and too see how long that takes.

If I wanted to connect to a different end point to pull down more information on an individual surf spot I could that by handling the OnItemClick event for the List View.

If there is interest I’m happy to share my Sync-Timer source code.

Posted in Android, iOS, Mobile, REST | 2 Comments

Delphi and REST Client Development

I really don’t like to make comparison between different development tools. In my opinion most tools have their strengths for certain tasks. So I try not to criticize other tools by name. Delphi is my favorite, and I believe it is the best all around tool for more projects I face. Occasionally I have someone ask me how Delphi compares to another tool they are evaluating. I thought I would share one such experience.

When I was in Brazil I went to meet with a guy who was in charge of software development for a large, international company. They were all standardized on a different development tool, but it didn’t really have a mobile solution. They were looking at some tools that claimed to make it really easy to do mobile development, but once he got past the marketing he found it wasn’t all it was cracked up to be.

To evaluate the tool he decided he would develop a simple little app for iPhone and Android that connected to a REST server to pull down a list of countries, cities, etc. to help people find offices for his company. He thought that a week should be more then enough time to develop this simple application.

What he found was that while he expected it to be cross platform, it was actually only shared code. So he had to create a project for each platform, and then he could share part of the code (not as much as he would like) between each project. Also there was significant overhead in consuming a REST service. After a couple days he realized a week wasn’t enough time for even one platform.

To top it all off he discovered that the cost was actually way more then it was initially made out to be when you actually got down to doing something serious.

So I showed him Delphi and told him that the app he was describing could be done inside a day, probably with a prototype within an hour. He found that a little hard to believe, so I created a simple REST client displaying the result of a REST service and deployed it to my Android phone. It took about 5 minutes, including the time it took to connect to his WiFi.

He was so impressed he actually got his phone out to make a video of it just so he could show his boss, who he was sure wouldn’t believe him.

That is what I love about Delphi. It makes the simple things that you need to do all the time so simple and so quick, and it makes everything else still pretty simple. Need an API that isn’t wrapped in a component or the RTL, no problem, you can still access it.

Posted in REST, Tools | Tagged | 11 Comments

Android Services Presentations and Workarounds

Another round up of Android Service related resources.

For starters here are my slides from my Android Services presentation in Brazil.

You can also download the Workarounds and fixes for the Java templates and Android Manifest from CodeCentral.

You can catch a replay of Julian Mesa’s session from CodeRage X

And check out my other Android Services posts.

Posted in Android, Conferences | Tagged | Leave a comment

T-Shirts from Brazil Delphi Conference

I picked up some great T-Shirts during my visit to Brazil.

Delphi Developer: The evolution of mankind

Delphi Developer: The evolution of mankind

I really like this one as the Delphi Developer is the highest evolved form of developer. This is evident by not only their great taste in development tools, but also by the fact they are incredibly productive and produce great apps for all the platforms.

Aquasoft - coffeed powered developer

Aquasoft warns: This shirt contains a Delphi Developer. To run it, please fill with coffee and press F9.

I’ll take some Guaraná Antarctica instead of coffee though. Just not sure where my F9 key is located. Carlos from Aquasoft showed me a very cool ORM he is working on. Hopefully we will see some more of that in the near future.

Windows, Apple, Android = Delphi

Windows, Apple, Android = Delphi Developer

I love that Windows, Apple and Android are all following the Delphi Developer.


TDevRocks is a great blog by MVP Adriano Santos.

Posted in Conferences | 2 Comments

Connecting the Philips Hue Bridge to Wi-Fi

philips-hue-bridgeI needed to connect my Philips Hue Bridge to Wi-Fi to use it for a presentation at a conference in Brazil. Most public Wi-Fi networks segments each connection preventing communication between devices, besides the fact the Hue Bridge is Ethernet only (Not sure who at Phillips made that decision). Luckily I was able to come up with a solution.

All I needed was my MacBook Pro, which was connected to the conference WiFi and a Thunderbolt Ethernet adapter (which I always carry just incase there is wired Internet access).

Connect the Hue to the MacBook via the Thunderbolt adapter. Then under System Preferences -> Sharing. Select Internet Sharing. Connect from Wi-Fi to computers using Thunderbolt Ethernet (I usually have sharing set the otherway around). Then activate Internet Sharing (and verify you want to do it).

Sharing WiFi to Thunderbolt

This granted the Bridge access to the internet, and gave it a 192.x.x.x IP address that I could reach from my MacBook Pro (and the VM) to send commands to the bridge. This worked great for my presentation, but isn’t a solution for home connectivity.

Also, it turns out the Philips Hue Bridge I just purchased is now obsolete thanks to an Apple HomeKit requirement.

Posted in gadgets | Tagged | Leave a comment

New Delphi Seattle MongoDB Sample

I created some more Delphi 10 Seattle samples to show off MongoDB and FireDAC functionality: LocalSQL, Indexing & Geospatial.


The first one queries some data from MongoDB allowing you to specify the match, sort and projection, then it stores the results in a DataSet. At that point you can use LocalSQL to write a SQL query against the result set. While FireDAC gives you full native support for MongoDB, it also puts the SQL back into NoSQL.

MongoDB FireDAC LocalSQL

Indexing is used to improve your query performance. It is really easy to work with MongoDB queries with FireDAC.

MongoDB FireDAC Indexes

And one of the cool features of MongoDB is that you can do spatial queries. Here is an example that shows how to create a Spatial index and then do a spatial query with FireDAC. This uses the restaurant data that is included with the shipping samples, so make sure you load the restaurant data first.

Geospatial MongoDB FireDAC

If you missed my previous post I had a MongoDB FireDAC and C++Builder sample.

[You can download my new samples here.]

Posted in DataBase, Source Code | Tagged | Leave a comment

More Coding In Delphi in More Formats

More Coding in Delphi by Nick HodgesFor everyone who updated to Delphi or RAD Studio 10 Seattle, you get a free copy of Nick Hodges new book More Coding in Delphi (At least if you update before the special offer runs out.) Just yesterday this was updated to include the book in ePub and Mobi format, in addition to the PDF version. This is great if you have a Kindle (Mobi) or other eReader (ePub) since these formats are more flexible.

So Upgrade to RAD Studio 10 Seattle today and download More Coding in Delphi by Nick Hodges and load it on your favorite eReader. Then buy a print copy too, because those are easier to get autographed.

Posted in News | 1 Comment

Another Android Background Services Video

Here is a snippet of my video from our Saturday Deep Dive that covers Android Background Services and iOS Background Mode.

For more information check out the original blog post.


Posted in News | Tagged | Leave a comment