PhobosWatch: manifest => enum

"Jérôme M. Berger" jeberger at free.fr
Sat Dec 29 23:45:10 PST 2007


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Walter Bright wrote:
> What do we do with:
> 
>     const MyInt m = 3;
>     auto n = m;
> 
> Does n get const or not? If it is, now it is behaving differently from
> other types, and so is not a plug-in replacement. If it does not, what
> happens if MyInt has a pointer member? Suddenly, the const gets stripped
> from the pointer, and there is no const-correctness.

	OK, I see your point, but like Bill said I think the issue here is
with the "auto" type inference rather than the "const". What is
needed is for "auto" to be smarter than it is now, namely:

 - for atomic types, strip the const;
 - for pointers, if the language supports head const, then strip the
tailmost const (ie make a mutable pointer to const data) otherwise
keep the const;
 - for classes, keep the const;
 - for struct, the compiler already keeps track of whether the
struct contains pointers or not because of the GC, doesn't it? In
that case, if the struct contains pointers keep the const, and if it
doesn't strip it (1);
 - in all cases, allow a "const auto" statement to force an "auto"
declaration to be "const". This would work even if the right hand
expression is not "const":
	int x = 42;
	const auto y = x;       // y is now "const int"

		Jerome

(1) It would be even nicer if the compiler could keep track of
whether there are *mutable* pointers in the struct and strip the
"const" from the "auto" variable if all pointers in the struct are
"const" anyway, so it wouldn't break const-correctness:

struct {
   int value;
   const int* pointer;
} ConstStruct;

struct {
   int value;
   int* pointer;
} MutableStruct;

const ConstStruct   cs0;
const MutableStruct ms0;
auto cs1 = cs0;         // <= cs1 is a ConstStruct since all
                        // pointers inside are const anyway.
auto ms1 = ms0;         // <= ms1 is a const MutableStruct.

cs0.value = 0;          // Error, cs0 is const
cs1.value = 1;          // OK
ms0.value = 2;          // Error, ms0 is const
ms1.value = 3;          // Error, ms1 is const


(2) In all I've said, you can replace "const" with "invariant" and
the same arguments would apply.

- --
+------------------------- Jerome M. BERGER ---------------------+
|    mailto:jeberger at free.fr      | ICQ:    238062172            |
|    http://jeberger.free.fr/     | Jabber: jeberger at jabber.fr   |
+---------------------------------+------------------------------+
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.7 (GNU/Linux)

iD8DBQFHd0yGd0kWM4JG3k8RAqAYAJ9ZAd4zCHvNIaHBsf5zfRlKKJd6WACdGyOo
miKzodWMLGxjfeuJNO45Ig4=
=NjKB
-----END PGP SIGNATURE-----



More information about the Digitalmars-d mailing list