Easy Introspection
Relevant Logic icon

Relevant Logic


Easy Introspection

As part of our forthcoming book, REAL OOP with REALbasic, we've released an example REALbasic project illustrating the use of Introspection.

REAL has actually fully documented Introspection as far as we can tell (Woot!), but the design of the feature is, frankly, a bit weird. Why on earth they didn't base introspection on methods on Object and Delegate is beyond me.

The example project makes use of some cool features in REALbasic (Extends methods and Operator_Convert) to show how we can work around the bizarre design of Introspection in REALbasic, to get something much easier to use. For example, to get a list of the method names on an object, REAL wants you to do this:


Sub MethodNames(o As Object) As String()
  Dim ms() As Introspection.MethodInfo

  ms = Introspection.GetType(o).GetMethods

  Dim result() As String
  For Each m As Introspection.MethodInfo In ms
    result.Append m.Name
  Next

  Return result
End Sub

But with the provided code, you can do this:


Sub MethodNames(o As Object) As String()
  Dim result() As String
  For Each m As MethodInfo In o.Methods
    result.Append m.Name
  Next
  Return result
End Sub

The example also shows how you can use introspection to implement concise, readable (if slow!) functional programming style features. For example, you can write something very like the above as:


Sub MethodNames(o As Object) As Variant()
  Return o.Methods.Apply('Name')
End Sub

Worth noting that the example isn't complete: it is intended to illustrate how some of REALbasic's features can make using something complex like the Introspection features much easier to both read and write.

blog comments powered by Disqus