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

Michel Fortin michel.fortin at michelf.ca
Sat Jan 18 13:38:35 PST 2014


On 2014-01-18 21:05:01 +0000, Walter Bright <newshound2 at digitalmars.com> said:

> On 1/17/2014 7:05 AM, Michel Fortin wrote:
>> Some more thoughts.
> 
> The postfix ? has parsing issues with ?: expressions.

I thought that because the left hand side was a type it could be 
disambiguated. Somewhat like '*' is disambiguated between pointer 
declaration and multiplication depending on whether the left hand side 
is a type.

	int * a; // pointer declaration
	a * b; // multiplication

But I might be wrong, especially since ?: is a trinary operator while * 
is always binary.


> Aside from that, non-null is only one case of a universe of types that 
> are subsets of other types. I'd prefer a more general solution.

Hum, kind of thing do we want to support in a general solution?

The closest thing I can think of is range constrains. Here's an example 
(invented syntax):

	void test(int!0..10 a) // param a is in 0..10 range
	{
		int b = a; // fine
		int!0..5 c = a; // error, incompatible ranges
		int!0..5 d = a/2; // fine, can't escape range

		if (a < 5)
			d = a; // fine, 'a' is int$0..5 on this branch
	}

Obviously compiler code for range constrains and non-null pointers 
would be shared to some extent, as the rules are pretty similar.

But what other cases should be covered by a more general solution? Do 
we want to add a way for users to declare their own custom type 
modifiers that can do things? That depends on control flow?


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



More information about the Digitalmars-d mailing list