Just so you know

| | Comments (5)

Some people tend to forget this, so I figured I'd bring it up again. You can use the extends keyword to extend datatypes in REALbasic. So, for instance, you can extend the Integer datatype and it will work.

A good example of this is for those people who seem to think C is the gold standard and REALbasic requires increment and decrement operators. Using extends, that's trivial to do yourself:
[rbcode]
Sub Increment( ByRef extends i as Integer )
i = i + 1
End Sub

Sub Decrement( ByRef extends i as Integer )
i = i - 1
End Sub[/rbcode]
Another way that this is handy is to extend structures. Sometimes you'll run into times when you want a C union in your structure, like this:


typedef struct Test {
union field {
long l1;
short s1;
short s2;
}
} Test;

You can use that like this:

someTest.field.l1 = 0xFFFF1234;

if someTest.field.s1 == 0xFFFF { ... }

Basically, it allows l1, s1 and s2 to all occupy the same location within the structure. So assigning to one variable will alter the values of all the variables in the union (assuming they overlap). You can accomplish something like this in REALbasic easily enough using extends.
[rbcode]// Assumes that SomeTest as a member called Field which is appropriately sized
// for the union type and resides at the "offsetToField" byte offset.
Sub FieldL1( ByRef extends s as SomeTest, assigns i as Integer )
dim mb as MemoryBlock = s.StringValue( TargetLittleEndian )
mb.LittleEndian = TargetLittleEndian
mb.Long( offsetToField ) = i
s.StringValue( TargetLittleEndian ) = mb
End Sub

Sub FieldS1( ByRef extends s as SomeTest, assigns i as Short )
dim mb as MemoryBlock = s.StringValue( TargetLittleEndian )
mb.LittleEndian = TargetLittleEndian
mb.Short( offsetToField ) = i
s.StringValue( TargetLittleEndian ) = mb
End Sub

Sub FieldS2( ByRef extends s as SomeTest, assigns i as Short )
dim mb as MemoryBlock = s.StringValue( TargetLittleEndian )
mb.LittleEndian = TargetLittleEndian
mb.Short( offsetToField + 2 ) = i
s.StringValue( TargetLittleEndian ) = mb
End Sub[/rbcode]
It may not be ideal, or nearly as fast as the C code, but when reading the code, they'll look almost identical.

You get the picture of what I'm trying to say though: don't forget about extends, it can do some truly powerful stuff!

5 Comments

It would be a bit faster and a bit more useful if you could extend datatypes with inline/macro code. I am hoping this gets added to the language at some point (2007?).


function overworkDevelopers( extends oneDeveloper as REALEmployee )

if oneDeveloper.hasFreeTime then
workThemHarder
end if

Sounds like a good option :)


extends is one of those often overlooked items I'm sure

Norman, you mean "oneDeveloper.workLoad++," don't you? ;-)

or maybe

sub boss(extends hours as WorkDay)

hoursToWorkPerDay = hoursToWorkPerDay + 2
hoursAvailableTilNextDeadline = hoursAvailableTilNextDeadline - 48

end sub

Programmer humor... LOL! ;)

Leave a comment

Disclaimer

I'm currently an employee of REAL Software. My blog is mine. The opinions represented in this blog are mine as well and may not represent my employer's opinions. All original material is copyrighted and property of the author.

REALbasic® is a registered trademark of REAL Software, Inc. REAL SQL Server™ and Lingua™ are pending trademarks of REAL Software, Inc. All rights reserved.