D - Unsafe and doomed

Michel Fortin michel.fortin at michelf.ca
Sat Jan 4 08:27:20 PST 2014


On 2014-01-04 14:46:26 +0000, "Adam D. Ruppe" <destructionator at gmail.com> said:

> On Saturday, 4 January 2014 at 12:37:34 UTC, ilya-stromberg wrote:
>> Have you got any plans to impove this situation?
> 
> I wrote a NotNull struct for phobos that could catch that situation. I 
> don't think it got pulled though.
> 
> http://arsdnet.net/dcode/notnull.d
> 
> With @disable is becomes reasonably possible to restrict built in types 
> with wrapper structs. It isn't perfect but it isn't awful either.
> 
> The big thing people have asked for before is
> 
> Object foo;
> if(auto obj = checkNull(foo)) {
>     obj == NotNull!Object
> } else {
>    // foo is null
> }
> 
> and i haven't figured that out yet...

In my nice little C++ world where I'm abusing macros and for loops:

	#define IF_VALID(a) \
		if (auto __tmp_##a = a) \
			for (auto a = make_valid_ptr(__tmp_##a); __tmp_##a; __tmp_##a = nullptr)

Usage:

	T * ptr = ...something...;

	IF_VALID (ptr)
	{
		... here ptr is of type ValidPtr< T > which can't be null
		... can be passed to functions that wants a ValidPtr< T >
	}
	else
	{
		... here ptr is of type T*
	}

Can't do that in D.

-- 
Michel Fortin
michel.fortin at michelf.ca
http://michelf.ca



More information about the Digitalmars-d mailing list