Latest Clarion beta adds generics and other goodies

Published 2008-04-17    Printer-friendly version

Yesterday was a busy day. SoftVelocity released the latest C7 and Clarion# builds to beta testers, and posted no less than four blog entries covering the new features in the latest release.

First the bad news: no AppGen yet in the latest release (build 3276). But for the first time SV has released some information about the status of internal testing. Key points:

  • Some apps (and dictionaries) convert cleanly
  • Code generation is being tested
  • Editors/window and report designers are being integrated

Z didn't say but I assume that code generation is being tested against the existing ABC templates.

The latest build also sports a completely rewritten IDE parser. Besides some speed improvements this should significantly improve code completion, which hasn't been that reliable or effective in past releases. It also promises a more useful class browser.

The blog entry also mentions a new "file schema pad" which is like the table schematic in the C6 AppGen. The Dictionary Editor and Synchronizer are code complete and in testing.

The Data Diagrammer's new Reports View is a handy way to get a sorted view of all aspects of your dictionary. I won't go into that in any detail here - just check out the blog post.

Generics

One of Z's blog posts coveres the introduction of generic types, often just called generics. These types use a special syntax with angle brackets, as in

<type>

where type can be any type such a simple long or string, all the way up to any interface or class you create. This may not seem immediately useful to you, but generics are really, really handy. Think of a situation where you don't know ahead of time what kind of object you're going to want to use in a certain situation. In Clarion you have the ANY data type for this situation; in Clarion# you're more likely to use a reference of the type object, since all classes inherit from object (in Clarion#, ANY is actually the Clarion.ClaAny class).

If you create, for instance, a queue of object references, you can assign anything you want to each queue element. But when you want to use one of those objects, all your code will see is the methods and properties of the object class, not of your class. To actually "see" your class the code has to first type cast the object reference back to the right data type. In Clarion# you do this with the tryas operator:

myobj = q.obj tryas MyClass

Casts are expensive; the runtime environment has to do a bunch of checks to make sure that you can safely cast the object to the specified type. If you attempt the wrong kind of cast you'll get an exception. The power of type casting is also the danger - if you can cast to anything, you can also cast to the wrong thing.

Generics maintain the flexibility of using object references while enforcing a type you specify at runtime. In a sense, generics are simply type parameters; just as you pass data to a method, you pass type information to either the class as a whole or to a class's method (the class or the method must be set up to accept a type, of course). I'll only discuss the former approach here.

The idea of passing a type to a class is to tell the class that anywhere it has code that references a generic type (often written as <T>, assuming the class/method uses just one generic type) it should substitute the type you just gave it. Here's Z's example:

myStringList         List<string>
myIntList            List<Int32>
myPersonList         List<Person>
mp                   Person
    CODE
    myStringList = new List<string>()
    myStringList.Add('Joe')
    myStringList.Add('Jane')


    myIntList = new List<Int32>()
    myIntList.Add(2)
    myIntList.Add(3)
    myIntList.Add(4)
    myPersonList = new List<Person>()
    Loop i# = 1 to 5       
        mp = new Person()
        mp.Name = 'Joe' & i#
        myPersonList.Add(mp)
    End     

Keep in mind that List is really System.Collections.Generic.List. I could show you the C# class declaration, but for fun here's a Clarion equivalent of the List class declaration with the Add method:

List                class<T>
Add                     procedure(T item)
                    end


List<T>.add            procedure(T item)
    code
    ! Add the passed item to a collection

Actually that's not what List really would be like in Clarion because List also implements some generic interfaces, which I've left those off to avoid further confusion, and contains a bunch of additional methods. The point of this example (yes, there is a point) is that when you create a new List this way:

myStringList = new List<string>() 

you're telling List that wherever it sees <T> it should now see the string data type. And when you call the Add method, the T parameter is now a string parameter. That means that the class knows the data type you're passing, so it never has to do a cast when you want a string back.

That's a really quick intro to the idea of generics; there are a lot of variations on the theme. Generics are an important subject and one you can be sure will be covered in ClarionMag at some point, hopefully sooner rather than later. At this point generics and delegates are both pretty high on my hit list of Clarion# topics; I haven't sat down to either one yet so if someone out there would like to write an article or two on either of these (or both!) please let me know.

Printer-friendly version