LWN.net Logo

RE: nullable pointers

RE: nullable pointers

Posted Apr 12, 2011 16:04 UTC (Tue) by oelewapperke (guest, #74309)
In reply to: RE: nullable pointers by mister_m
Parent article: GCC 4.6.0 released

The essence of the trick is that you simply make a type difference between "filled in" pointers and "null" pointers.

A tree would be defined like so :

abstract class TreeNode {}
abstract class EmptyTreeNode extends TreeNode {}
abstract class FilledTreeNode extends TreeNode {
TreeNode first;
TreeNode second;
}

If you combine this with runtime polymorphism you can do something like this (won't work in Java, but I'm using java code anyway)

void processTreeNode(EmptyTreeNode et) {}
void processTreeNode(FilledTreeNode ft) {
// you might want to do something useful here
processTreeNode(ft.first);
processTreeNode(ft.second);
// or here
}

then simply call processTreeNode(tree), and the compiler would figure out whether to call the first or the second function.

I would love for java to support this though.


(Log in to post comments)

Copyright © 2013, Eklektix, Inc.
Comments and public postings are copyrighted by their creators.
Linux is a registered trademark of Linus Torvalds