What's This Do?

Programming etc.

UUIDs in Ada

If programming languages were high schoolers, Ada would be the nerd. When C++, Java and C# are busy putting on makeup or riding in cars with Lambdas, Ada is in the library perfecting her type-safety and studying up on mission-critical systems. Ada is the girl that everyone’s heard of, but not many people know or care about. As a result, she’s incredibly smart, having spent nearly all of her time ensuring that planes don’t fall out of the sky and that nuclear reactors stay cool, but she’s also incredibly socially awkward.

Ada is simply not as popular in the social world of coding, where C++, C# and Python (to name a few) reign supreme. And sure, you can’t beat Python’s flexibility and feature-rich library. C++ has a generic feature (templates) which forms a Turing-complete language of its own. C#’s dynamic types, coupled with lambda’s and easy-to-use parallelism is a joy to code with. And what does Ada have…?

procedure Temperatures is

    package Cores is
        type Safe_Temperature is range 0 .. 25;
        procedure Set_Temperature(t : in Safe_Temperature; c : in out Core);
    end Cores;

    package body Cores is
    begin
        procedure Set_Temperature(t : in Safe_Temperature) is
        begin
            -- Set some temperature here
        end Set_Temperature;
    end Cores;

    use Cores;
    bad_temp : Integer := 100;
    good_temp : Safe_Temperature := 1;
begin
    Set_Temperature(good_temp); -- OK
    Set_Temperature(bad_temp); -- BAM! Type-safety
end Temperatures;

That’s right, unless you call Set_Temperature with a Safe_Temperature, your code won’t compile. And this doesn’t require template tricks like in C++, this is a core feature of the language. It’s the whole damn point of Ada in the first place. Sure, you can cast that Integer to a Safe_Temperature, but a runtime exception will be thrown in the case where the value does not fall within the specified range. Thus, through the type declaration of Safe_Temperature, you can ensure that you will never be able to set an UNSAFE temperature, without having to manually code the runtime checks. Ada just got a bit sexier, didn’t it?

But Ada remains socially underdeveloped, which means that the number of high-quality, open-source libraries available for use in Ada projects is small compared to languages like Java, C++ or C#. This provides a big opportunity for writing these support libraries yourself. For example, I couldn’t find a decent UUID library for Ada, so I wrote one myself. I call it AdaID and it’s hosted on github.

AdaID allows you to create unique identifiers, either randomly or name-based and convert them to a Hex-based string. In the near future it will also allow you to re-create UUIDs from a string representation. Here’s an example of what you can do with AdaID right now:

with AdaID; use AdaID;
with Ada.Text_IO; use Ada.Text_IO;
procedure Print_Random_UUID is
    id : UUID;
begin
    Random(id);
    Put_Line(To_String(id));
end Print_Random_UUID;

So come on, jump on board the Ada bandwagon!

About these ads

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 189 other followers

%d bloggers like this: