Records on Steroids

Another relevant family of value types is represented by structures in C jargon, or records as they are called in Delphi. If records have always been part of the language, in this version they gain a lot of new ground as records can now have methods associated with them and even operators, as we'll see later on in this chapter . A record with methods is somewhat similar to a class the most relevant difference beside the lack of inheritance and polymorphisms is that record type variables use...

Delphi New Predefined Records

The presence of a sophisticated record type definition and of the operators overloading has led Borland to change a lot of predefined Delphi types turning them into records. Notable examples are the types variant, datetime, and currency. Variants in particular represent another data type not part of the CLR foundations that Borland has been able to redefine on top of the available CLR features without affecting the syntax and the semantic of the existing code. The implementation of variants on...

Events for Everybody

In its first incarnation, Delphi introduced the idea of events as most development tool use it nowadays. An event in Delphi provides a way to hook an external method to an object, thus modifying the object's behavior through delegation instead of customizing its class through inheritance . For example, the code a button executes when it is clicked in not written in the button class, but the button delegates to a method of another object, usually the form hosting the button. Technically, in...

Strings

Strings are just strings and considering that Delphi long strings are allocated on the heap, reference counted and use the copy-on-write technique, .NET strings will be quite familiar. There are a few differences, though. The first is that strings on .NET use UTF16 Unicode characters, that is each character is represented with 16 bits of data 2 bytes . This is transparent, as when you index into a string looking for a given character, the index will be that of the character, not that of the...

Sealed Classes and Final Methods

In short, sealed classes and classes you cannot further inherit from, while final methods are virtual methods you cannot further override in inherited classes. This is the syntax of a sealed class taken from the SealedAndFinal example Trying to inherit form it causes the error Cannot extend sealed class TDerivl. This is the syntax Inheriting from this class and overriding the A method causes the compiler error Cannot override a final method. Now, again we can ask ourselves what is the rationale...

Indexers or Array Properties

Since its first release, Delphi has always supported the idea of array properties that receive as parameter a value passed among square brackets. And we've always enjoyed the possibility of marking one of the array properties of a class as default, so that it can be referenced applying the square brackets right to the object, omitting the property name. So for example The C language has a very similar idea, called indexer, with a significant difference you can have multiple indexers that is...

Untyped Parameters

Another risky technique is the use of untyped parameters in procedures. Here is an example again from the UnsafeTest project that works procedure UnsafeParam var param begin end test string n Integer obj TObject begin test 'foo' UnsafeParam test n 23 In this case you'll get the output you'd expect and no compile time warning or runtime error before you get too surprise by this behavior, notice that the compiler generates a method with the following signature, which has an object passed by...

Primitive Types

The .NET CLR defines quite a few primitive types, which are not natively mapped to objects but a direct representation not predefined, as it can change with the target CPU and operating system version, for example on a 32biit or 64bit CPU . The CLR primitive types are mapped to corresponding Delphi types, as the following table highlights. Borland.Delphi.System.Currency a record based on Decimal What you can notice is that not all Delphi types have a CLR equivalent. You are recommended not to...

When Private is Really Private

Differently from most other OOP languages, Delphi had a more relaxed rule for private visibility. In fact, access specifiers where enforced only across units and not for classes within the same unit. In other words, a class could access to private fields and methods of another class defined in the same unit. The same happened with protected members. The first important thing to consider is that to maintain source code compatibility the behavior of the private and protected keywords has not...