www.digitalmars.com         C & C++   DMDScript  

D - static_cast

reply "Vathix" <vathix dprogramming.com> writes:
It might be good to have a static_cast for classes when you know the cast is
valid. It could still make sure in debug mode and assert it.

class Foo
{
protected Foo[] foos;
}

class Bar: Foo
{
void addBar(Bar bar)
{
 foos.length = foos.length + 1;
 foos[foos.length - 1] = bar;
}
void something()
{
 uint i;
 for(i = 0; i != foos.length; i++)
 {
  /*
  I know all these foos are (at the) Bar
  */
  (static_cast(Bar)foos[i]).something();
 }
}
}
Aug 10 2003
parent reply "Matthew Wilson" <matthew stlsoft.org> writes:
I have no opinion on the merits of this, but I would say that the last thing
D needs is another place for round brackets. Talk about hard to parse! (I'm
talking about code inspectors here, I have no expertise in compiler writing)

There's a reason C++ uses cast_name<type>

"Vathix" <vathix dprogramming.com> wrote in message
news:bh7bc2$6j0$1 digitaldaemon.com...
 It might be good to have a static_cast for classes when you know the cast

 valid. It could still make sure in debug mode and assert it.

 class Foo
 {
 protected Foo[] foos;
 }

 class Bar: Foo
 {
 void addBar(Bar bar)
 {
  foos.length = foos.length + 1;
  foos[foos.length - 1] = bar;
 }
 void something()
 {
  uint i;
  for(i = 0; i != foos.length; i++)
  {
   /*
   I know all these foos are (at the) Bar
   */
   (static_cast(Bar)foos[i]).something();
  }
 }
 }

Aug 10 2003
next sibling parent Ilya Minkov <midiclub 8ung.at> writes:
Matthew Wilson wrote:
 I have no opinion on the merits of this, but I would say that the last thing
 D needs is another place for round brackets. Talk about hard to parse! (I'm
 talking about code inspectors here, I have no expertise in compiler writing)

When parsing from left to right per recursive descend, it predicts perfectly well - the first token tells you what comes next. At least, not at all worse than the current cast syntax. And both do *much* better than C-like cast, which i'd prefer to see deprecated. As to the merit of this, i don't think there is much sense. I can imagine utility for that in a language without templates.
 There's a reason C++ uses cast_name<type>

This form doesn't make it any better. -i.
Aug 11 2003
prev sibling parent "Mike Wynn" <mike.wynn l8night.co.uk> writes:
"Matthew Wilson" <matthew stlsoft.org> wrote in message
news:bh7cqv$7v3$2 digitaldaemon.com...
 I have no opinion on the merits of this, but I would say that the last

 D needs is another place for round brackets. Talk about hard to parse!

 talking about code inspectors here, I have no expertise in compiler

   (static_cast(Bar)foos[i]).something();

not that I think static_cast is needed, but its no different from cast(type) I think you are confusing the precidence forcing brackets with those used for the cast! D (cast(Bar)foos[i]).something(); C ((Bar)foos[i]).something(); can't remember if D does cast(Bar)foos[i].something(); like C ((Bar)foos[i]).something(); or Java (Bar)(foos[i].something());
Aug 11 2003