Non-null objects, the Null Object pattern, and T.init

H. S. Teoh hsteoh at quickfur.ath.cx
Fri Jan 17 10:11:30 PST 2014


On Fri, Jan 17, 2014 at 10:00:26AM -0800, H. S. Teoh wrote:
[...]
> If Andrei's proposal were extended so that .init can be overridden by a
> member *function*, then this would work:
> 
> 	class NullTracker {
> 		override typeof(this) init(string _file=__FILE__,
> 						size_t _line = __LINE__)
> 		{
> 			class Impl : NullTracker {
> 				string file;
> 				size_t line;
> 				this(string f, size_t l) { file=f; line=l; }
> 
> 				override void method1() { nullDeref(); }
> 				override void method2() { nullDeref(); }
> 				...
> 				void nullDeref() {
> 					// N.B.: puts the *source* of the
> 					// null in the Exception.
> 					throw new Exception(
> 						"Null dereference",
> 						file, line);
[...]

P.S. A better message might be "Null dereference, uninitialized object
from %s:%d", to distinguish the site of the null dereference vs. its
ultimate source.


T

-- 
"How are you doing?" "Doing what?"


More information about the Digitalmars-d mailing list