www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - The new invariant.

reply Chris Nicholson-Sauls <ibisbasenji gmail.com> writes:
A couple of quick questions about the upcoming 'invariant' type constructor. 
Given a 
class Foo, will there be a way to mark the class as having only invariant
instances?  Ie 
can I declare it as 'invariant class Foo {}' much as 'scope class Foo {}' would
make it 
RAII-only?

Second, say I want to make an associative array of invariant Foo instances. 
How would I 
declare this?  (Let's use int as the key type, for simplicity.)
invariant Foo[int] pool ; // reads to me as the AA type is invariant, which is
useless
invariant(Foo)[int] pool ; // perhaps?  like with the new const()?

This would be quite nifty for one of my projects, if it works like I expect.

private static invariant(Symbol)[invariant(char[])] pool ;

It'd be beautiful.  Though I still worry a little about the current invariant{}
contract 
being broken.

-- Chris Nicholson-Sauls
Mar 25 2007
parent reply "Andrei Alexandrescu (See Website For Email)" <SeeWebsiteForEmail erdani.org> writes:
Chris Nicholson-Sauls wrote:
 A couple of quick questions about the upcoming 'invariant' type 
 constructor.  Given a class Foo, will there be a way to mark the class 
 as having only invariant instances?  Ie can I declare it as 'invariant 
 class Foo {}' much as 'scope class Foo {}' would make it RAII-only?
Yes.
 Second, say I want to make an associative array of invariant Foo 
 instances.  How would I declare this?  (Let's use int as the key type, 
 for simplicity.)
 invariant Foo[int] pool ; // reads to me as the AA type is invariant, 
 which is useless
 invariant(Foo)[int] pool ; // perhaps?  like with the new const()?
The latter.
 This would be quite nifty for one of my projects, if it works like I 
 expect.
 
 private static invariant(Symbol)[invariant(char[])] pool ;
That too. Possibly we'll also alias invariant(char[]) to string.
 It'd be beautiful.  Though I still worry a little about the current 
 invariant{} contract being broken.
We're looking into it. Andrei
Mar 25 2007
parent Chris Nicholson-Sauls <ibisbasenji gmail.com> writes:
Andrei Alexandrescu (See Website For Email) wrote:
 Chris Nicholson-Sauls wrote:
 A couple of quick questions about the upcoming 'invariant' type 
 constructor.  Given a class Foo, will there be a way to mark the class 
 as having only invariant instances?  Ie can I declare it as 'invariant 
 class Foo {}' much as 'scope class Foo {}' would make it RAII-only?
Yes.
Nice.
 Second, say I want to make an associative array of invariant Foo 
 instances.  How would I declare this?  (Let's use int as the key type, 
 for simplicity.)
 invariant Foo[int] pool ; // reads to me as the AA type is invariant, 
 which is useless
 invariant(Foo)[int] pool ; // perhaps?  like with the new const()?
The latter.
 This would be quite nifty for one of my projects, if it works like I 
 expect.

 private static invariant(Symbol)[invariant(char[])] pool ;
That too. Possibly we'll also alias invariant(char[]) to string.
Sweet. This sort of thing alone will make the new const'ness material well worthwhile for myself. -- Chris Nicholson-Sauls
Mar 25 2007