On 15/01/2009, at 1:32 AM, Adelle Hartley wrote:

Shortly after that discussion, it occurred to me to add a method to a called "try" which acts as a nil-object-style wrapper for all of a's other properties, so that I can write

 a.try.b.c.d

In the event that a.b returns a null value, a.try.b returns an object that responds to the same methods and properties as the expected type of b but which simply returns null on any method or property.

This is the Null Object design pattern:

  http://en.wikipedia.org/wiki/Null_Object_pattern

(It's also present in Haskell as the Maybe monad.)

Objective-C has a pretty interesting feature, which is that invoking a method on a nil object returns the equivalent of "zero" for the return type. (i.e. If the return type is a pointer, you get NULL back; for an int/long/etc, it'd be 0; for a float, 0.0, etc.) If you tried your a.b.c.d example in Objective-C, you'd simply get nil back for the final result. The Null Object design pattern was a conscious part of the language design.

Speaking of which, why aren't there more "mixed" strong/weak typing languages that compile to native code?

Usually, the reason you compile code is for performance, and static typing goes hand-in-hand with fast performance. Dynamic typing requires some sort of uniform ("boxed") representations of data where each object has a tag associated with it to indicate what type it is, along with code to check the tag is correct every time the object's accessed if you don't want a segfault. That lowers performance, which is a reason why most compiled languages are statically typed.


--
% Andre Pang : trust.in.love.to.save  <http://www.algorithm.com.au/>



Attachment: smime.p7s
Description: S/MIME cryptographic signature

_______________________________________________
coders mailing list
coders@slug.org.au
http://lists.slug.org.au/listinfo/coders

Reply via email to