Hiding class pointers -- was it a good idea?

eao197 eao197 at intervale.ru
Fri Aug 17 22:52:16 PDT 2007


I'm sorry for my agressive behaviour, I was wrong.

On Fri, 17 Aug 2007 09:25:34 +0400, eao197 <eao197 at intervale.ru> wrote:

> I think if there was only one reference/pointer type in D than it would  
> be easier to add such non-null references into the language. For  
> example, it is possible to use another symbol for pointers:
>
> class C {};
> C # non_null = ...; // Can't be null.
> C * nullable = ...; // Can be null.
>
> void f( C # non_null_arg ) { ... } // non_null_arg can't be null.
>
> non_null = nullable; // Error! Checked by compiler.
> f( nullable ); // Error! Checked by compiler.
> nullable = non_null; // Ok.
> f( non_null ); // Ok.
> if( nullable !is null )
>    f( nullable ); // Ok. In this branch nullable is not null.
>

I totally forgot about operator overloading. So it is obviously necessary  
to have not only values and pointers, but also references.

I think that if introduce special syntax for references than problem for  
non-null references could be solved. Moreover with explicitely defined  
references it is possible to remove 'in'/'out' argument modifiers.

For example, let use '@' as nullable reference and '#' as non-null  
reference:

int i; // Value.
int @ i; // Nullable reference to int.
int # i = ...; // Non-null reference to int. Must be initialized.
int * p; // Nullable pointer to int.

struct S;
S s; // Value.
S @ s;
S # s = ...;
S * s;

class C;
C c; // Error! C is a reference type.
C @ c;
C # c = ...;
C * c;

// Various kinds of arguments.
void f(
	// Pass by value.
	S a1,
	// Pass by reference, like 'out' argument.
	S @ a2,
	// Pass by reference, like 'in' argument.
	const(S) @ a3,
	// Same as two previous, but require non-null references.
	S # a4,
	const(S) # a5,
	// Pass by pointer, like 'out' argument.
	S * a6,
	// Pass by pointer, like 'in' argument.
	const(S) * a7 );

I don't think that non-null pointer has much sence. And if we try to  
introduce non-null pointers we can end up with things like non-null  
pointer to nullable pointer to non-null pointer to something.

Unfortunately, syntax 'C @ c' is ugly in comparision with current 'C c'.  
But I hope it make type system more consistent -- it is easy to  
distinguish values form references in code.

-- 
Regards,
Yauheni Akhotnikau



More information about the Digitalmars-d mailing list