keywords "objconst" and "objimmutable" vs. const(Object) ref

Michel Fortin michel.fortin at michelf.com
Wed May 18 08:35:02 PDT 2011


On 2011-05-18 10:46:19 -0400, Timon Gehr <timon.gehr at gmx.ch> said:

> Given that reference types do not support the semantics asked for at all (with
> transitive const/immutable), I think this is as consistent as it can get. (also
> way better than "obj<insert storage specifier here>" all over the place.)
> Am I correct when I assume that, if Foo is a class then
> 
> Foo ref is the same type as Foo,

Yes.

> but const(Foo ref)ref is not a valid type while const(Foo) ref is a valid type?

Yes and no. With my patch, "const(Foo ref)ref" is invalid:

	const(Foo ref)ref o; // error

But that's only disallowed at the syntactic level. Semantically, it 
works, so if you use some kind of indirection. So with an alias it's 
still allowed:

	alias Foo ref F;
	const(F)ref o; // ok


> Are there any strange implications for pattern matching?
> 
> For example:
> 
> alias const(Foo)ref T;
> static if(is(T U : const(U)ref){
>     //would it match? If yes, what is U? Foo ref?
> }

U should be "Foo", which is indeed synonymous to "Foo ref". I admit 
it's a little strange, but I don't think there's a better way to do it.

Unfortunately, your example above isn't taken care of correctly with my 
patch. There'll still be a couple of things to fix regarding type 
deduction, like the one above. But type deduction doesn't work too well 
in a lot of cases for pointer either, making it hard for me to make it 
work right for "Object ref". See my test case for this in the pull 
request, each line with a FIXME comment is (in my opinion) a bug:
<https://github.com/D-Programming-Language/dmd/pull/3/files#diff-21>

I think pointer type deduction and Object ref type deduction will 
require some more work. In theory they should work pretty much the 
same, except for the strangeness you noted above where "Foo" is the 
same as "Foo ref".

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



More information about the Digitalmars-d mailing list