Posted Mar 31, 2011 19:04 UTC (Thu) by mister_m (guest, #72633)
[Link]
I am a bit of a novice. Would you mind explaining how I would enable the compiler to do that? I am missing some programming knowlege in this area I think = perhaps a suer defined type? In school, we always set unused pointers to null, and then check for null to see if we can dereference whatever is at the other end.
RE: nullable pointers
Posted Mar 31, 2011 20:36 UTC (Thu) by HelloWorld (guest, #56129)
[Link]
> I am a bit of a novice. Would you mind explaining how I would enable the compiler to do that?
Unless you're the one writing the compiler, you can't. Of course, you could just use a language that does this by default, like Haskell or OCaml.
RE: nullable pointers
Posted Apr 12, 2011 16:04 UTC (Tue) by oelewapperke (guest, #74309)
[Link]
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.