www.digitalmars.com         C & C++   DMDScript  

D - auto instance syntax

reply "Matthew Wilson" <dmd synesis.com.au> writes:
I don't doubt that this issue has been amply covered over the last six
months, but I'd be very grateful if anyone could outline the reasoning
anyway.

Basically, I'm making a set of related - by form/function, not
polymorphically - classes and have created a template that will operate
equivalently on any of them. One of these templates is a scoping class (i.e.
applies the RAII idiom to the instances), such as the following:

class Type1
{
  . . .

  void start()
  { . . . }
  void stop()
  { . . . }
}

class Type2
{
  . . .

  void start()
  { . . . }
  void stop()
  { . . . }
}


template TypeModulators(T)
{
  . . .

  auto class Scope
  {
  public:
    this(T t)
    {
      m_t = t;

      m_t.start();
    }
    ~this()
    {
      m_t.stop();
    }

  private:
    T m_t;
  }

}


My question/issue/whatever is that to use it I have to type

  void RunType1(Type1 t1)
  {
    instance TypeModulators(Type1) TypeMods;

    auto TypeMods.Scope = new TypeMods.Scope(t1);

    . . . // Do some other stuff with t1
  }

which seems an awful lot of stuff to type, both in terms of the
instance-iation of the template, and the need to "new" the instance.

Am I correct in that this is the correct and minimal form?
Does anyone else think this is too much to type for the required operation,
and/or that the "new" operator seems wrong?
Is the book closed on this?
Finally, is there a good reason that we cannot parameterise the
instantiation on the instance - t1 - rather than the type - Type1 - as this
could be deducible anyway.

For myself, I'd like to see something like the following:

  auto TypeModulators(Type1).Scope scope = new
TypeModulators(Type1).Scope(t1);

or, better

  auto TypeModulators(Type1).Scope scope(t1);

or, potentially slightly better still

  auto TypeModulators(t1).Scope scope(t1);

Please feel free to flame me to death. :)

Matthew
Mar 17 2003
next sibling parent "Sean L. Palmer" <seanpalmer directvinternet.com> writes:
I agree, explicit template instantiation syntax seems really clunky to those
of us familiar with C++.

Lack of ctors and dtors on structs seems like a great loss as well.

Walter says these two things greatly simplify the compiler implementation.
It doesn't seem worth it to me.

Sean

"Matthew Wilson" <dmd synesis.com.au> wrote in message
news:b54685$1klk$1 digitaldaemon.com...
 I don't doubt that this issue has been amply covered over the last six
 months, but I'd be very grateful if anyone could outline the reasoning
 anyway.

 Basically, I'm making a set of related - by form/function, not
 polymorphically - classes and have created a template that will operate
 equivalently on any of them. One of these templates is a scoping class
(i.e.
 applies the RAII idiom to the instances), such as the following:

 class Type1
 {
   . . .

   void start()
   { . . . }
   void stop()
   { . . . }
 }

 class Type2
 {
   . . .

   void start()
   { . . . }
   void stop()
   { . . . }
 }


 template TypeModulators(T)
 {
   . . .

   auto class Scope
   {
   public:
     this(T t)
     {
       m_t = t;

       m_t.start();
     }
     ~this()
     {
       m_t.stop();
     }

   private:
     T m_t;
   }

 }


 My question/issue/whatever is that to use it I have to type

   void RunType1(Type1 t1)
   {
     instance TypeModulators(Type1) TypeMods;

     auto TypeMods.Scope = new TypeMods.Scope(t1);

     . . . // Do some other stuff with t1
   }

 which seems an awful lot of stuff to type, both in terms of the
 instance-iation of the template, and the need to "new" the instance.

 Am I correct in that this is the correct and minimal form?
 Does anyone else think this is too much to type for the required
operation,
 and/or that the "new" operator seems wrong?
 Is the book closed on this?
 Finally, is there a good reason that we cannot parameterise the
 instantiation on the instance - t1 - rather than the type - Type1 - as
this
 could be deducible anyway.

 For myself, I'd like to see something like the following:

   auto TypeModulators(Type1).Scope scope = new
 TypeModulators(Type1).Scope(t1);

 or, better

   auto TypeModulators(Type1).Scope scope(t1);

 or, potentially slightly better still

   auto TypeModulators(t1).Scope scope(t1);

 Please feel free to flame me to death. :)

 Matthew
Mar 17 2003
prev sibling parent reply "Walter" <walter digitalmars.com> writes:
"Matthew Wilson" <dmd synesis.com.au> wrote in message
news:b54685$1klk$1 digitaldaemon.com...
 My question/issue/whatever is that to use it I have to type

   void RunType1(Type1 t1)
   {
     instance TypeModulators(Type1) TypeMods;

     auto TypeMods.Scope = new TypeMods.Scope(t1);

     . . . // Do some other stuff with t1
   }

 which seems an awful lot of stuff to type, both in terms of the
 instance-iation of the template, and the need to "new" the instance.

 Am I correct in that this is the correct and minimal form?
No. You could also write: void RunType1(Type1 t1) { instance TypeModulators(Type1).Scope = new instance TypeModulators(Type1).Scope(t1); . . . // Do some other stuff with t1 } Note that the class is auto, so adding the auto again in the declaration is redundant.
 Does anyone else think this is too much to type for the required
operation,
 and/or that the "new" operator seems wrong?
 Is the book closed on this?
 Finally, is there a good reason that we cannot parameterise the
 instantiation on the instance - t1 - rather than the type - Type1 - as
this
 could be deducible anyway.
Mainly for error detection, as mixing up value and type arguments is a likely source of error.
Mar 18 2003
next sibling parent reply Burton Radons <loth users.sourceforge.net> writes:
Walter wrote:
 "Matthew Wilson" <dmd synesis.com.au> wrote in message
 news:b54685$1klk$1 digitaldaemon.com...
 
My question/issue/whatever is that to use it I have to type

  void RunType1(Type1 t1)
  {
    instance TypeModulators(Type1) TypeMods;

    auto TypeMods.Scope = new TypeMods.Scope(t1);

    . . . // Do some other stuff with t1
  }

which seems an awful lot of stuff to type, both in terms of the
instance-iation of the template, and the need to "new" the instance.

Am I correct in that this is the correct and minimal form?
No. You could also write: void RunType1(Type1 t1) { instance TypeModulators(Type1).Scope = new instance TypeModulators(Type1).Scope(t1); . . . // Do some other stuff with t1 } Note that the class is auto, so adding the auto again in the declaration is redundant.
Uhhhh... sometimes I think there's two Walters. The "auto"'s not redundant, it's required. This code: auto class C { } int main() { C c = new C; return 0; } Will complain with "variable c reference to auto class must be auto". It's a good error, I should opt-in for implicit code.
Mar 18 2003
parent "Walter" <walter digitalmars.com> writes:
Oops, you're right.
Mar 31 2003
prev sibling parent "Matthew Wilson" <dmd synesis.com.au> writes:
I get a compilation error if I don't declare as auto an instance of an auto
class. (Though I've not confirmed it with the particular combination of
template instantiation and variable initialisation as you've discussed here.
I presume you're talking about the general case - wherein I've had the
error. If not, why the difference)


"Walter" <walter digitalmars.com> wrote in message
news:b58slg$1ulv$1 digitaldaemon.com...
 "Matthew Wilson" <dmd synesis.com.au> wrote in message
 news:b54685$1klk$1 digitaldaemon.com...
 My question/issue/whatever is that to use it I have to type

   void RunType1(Type1 t1)
   {
     instance TypeModulators(Type1) TypeMods;

     auto TypeMods.Scope = new TypeMods.Scope(t1);

     . . . // Do some other stuff with t1
   }

 which seems an awful lot of stuff to type, both in terms of the
 instance-iation of the template, and the need to "new" the instance.

 Am I correct in that this is the correct and minimal form?
No. You could also write: void RunType1(Type1 t1) { instance TypeModulators(Type1).Scope = new instance TypeModulators(Type1).Scope(t1); . . . // Do some other stuff with t1 } Note that the class is auto, so adding the auto again in the declaration
is
 redundant.


 Does anyone else think this is too much to type for the required
operation,
 and/or that the "new" operator seems wrong?
 Is the book closed on this?
 Finally, is there a good reason that we cannot parameterise the
 instantiation on the instance - t1 - rather than the type - Type1 - as
this
 could be deducible anyway.
Mainly for error detection, as mixing up value and type arguments is a likely source of error.
Mar 18 2003