object initialisers

2003-06-12 Thread Nicholas Clark
IIRC objects are in the future. However, I'm currently writing C++ and
keep finding something annoying that I'd like to be able to do easily.

I have objects with attributes, such as

class Foo {
...
std::size_t spare;
std::size_t allocate;
std::size_t min_readline;

and then I have lots of constructors

  public:
Foo (...) ...  spare(0), allocate (4096), min_readline(80) ...

Foo (..., ...) ...  spare(0), allocate (4096), min_readline(80) ...

Foo (..., ..., ...) ...  spare(0), allocate (4096), min_readline(80) ...


As far as I can tell the only way to say spare should default to 0 is
to spell it out in each and every constructor. I'd like to write


class Foo {
...
std::size_t spare = 0
std::size_t allocate = 4096
std::size_t min_readline = 80

and have the compiler know that if I specify a member initialiser in my
my constructor, then that should be used, otherwise to default to using
the value I say. (To avoid the inevitable search/replace if I want to
change that value)

I can't figure out if there is a way to do this in C++ (the above syntax
isn't legal) and I'd like perl6 objects to make this sort of thing easy.

Nicholas Clark


Re: object initialisers

2003-06-12 Thread Rafael Garcia-Suarez
Nicholas Clark wrote:
 
 class Foo {
 ...
 std::size_t spare = 0
 std::size_t allocate = 4096
 std::size_t min_readline = 80
 
 and have the compiler know that if I specify a member initialiser in my
 my constructor, then that should be used, otherwise to default to using
 the value I say. (To avoid the inevitable search/replace if I want to
 change that value)

That's actually valid Java syntax (modulo a trailing C;). The default
value can be any expression (as long as it doesn't involve a reference
to the object being constructed, IIRC).

In fact Java provides a notion of instance initialiser, run before
any specific constructor you're invoking, that initialises such fields,
and may run other code.

The details aren't very clear to me, since I haven't written any Java
code since almost two years. I'm sure someone will correct me if I'm
wrong /-/.


an idle question: returning from a nested call

2003-06-12 Thread David Storrs
So, as I sweat here in the salt mines of C++, longing for the
cleansing joy that Perl(5 or 6, I'd even take 4) is, I find myself
with the following problem:

Frequently, I find myself writing stuff like this:

void Ficp400::SaveRow(long p_row) 
{
// if p_row is marked as deleted, return
if (GetStatus(row)  FLX_ROW_DELETE) { return; }

...
}

As a general rule, I don't like comments.  When I see a comment, I
want to turn it into a function name.  So, I keep wanting to be able
to write the above code like so:



void Ficp400::SaveRow(long p_row) 
{
Return_If_Is_Deleted(p_row);

...
}

Now, in C++ (or P6, FTM), I could make this work via a macro, but
that's ugly.  In P6, I could make it work by passing the current
continuation down to Return_If_Is_Deleted and call the continuation if
the row is in fact deleted, but that will require an extra param.  Is
there a way to make it work as written?  I'm thinking maybe the
Ccaller object would have something that would allow me to jump to
the right point (i.e., caller[2]).

Just an idle thought,

--Dks


Re: an idle question: returning from a nested call

2003-06-12 Thread Austin Hastings

--- David Storrs [EMAIL PROTECTED] wrote:
 So, as I sweat here in the salt mines of C++, longing for the
 cleansing joy that Perl(5 or 6, I'd even take 4) is, I find myself
 with the following problem:
 
 Frequently, I find myself writing stuff like this:
 
 void Ficp400::SaveRow(long p_row) 
 {
   // if p_row is marked as deleted, return
   if (GetStatus(row)  FLX_ROW_DELETE) { return; }
 
   ...
 }
 
 As a general rule, I don't like comments.  When I see a comment, I
 want to turn it into a function name.  So, I keep wanting to be able
 to write the above code like so:
 
 
 
 void Ficp400::SaveRow(long p_row) 
 {
   Return_If_Is_Deleted(p_row);
 
   ...
 }
 

sub Ficp400::SaveRow(Int $p_row)
{
  return if IsDeleted($p_row);
}




Re: an idle question: returning from a nested call

2003-06-12 Thread Austin Hastings

--- Austin Hastings [EMAIL PROTECTED] wrote:
 
 --- David Storrs [EMAIL PROTECTED] wrote:
  So, as I sweat here in the salt mines of C++, longing for the
  cleansing joy that Perl(5 or 6, I'd even take 4) is, I find myself
  with the following problem:
  
  Frequently, I find myself writing stuff like this:
  
  void Ficp400::SaveRow(long p_row) 
  {
  // if p_row is marked as deleted, return
  if (GetStatus(row)  FLX_ROW_DELETE) { return; }
  
  ...
  }
  
  As a general rule, I don't like comments.  When I see a comment, I
  want to turn it into a function name.  So, I keep wanting to be
 able
  to write the above code like so:
  
  
  
  void Ficp400::SaveRow(long p_row) 
  {
  Return_If_Is_Deleted(p_row);
  
  ...
  }
  
 
 sub Ficp400::SaveRow(Int $p_row)
 {
   return if IsDeleted($p_row);
 }

But if you really need to make a function out of it, see the Cleave
keyword.

=Austin



Re: an idle question: returning from a nested call

2003-06-12 Thread David Storrs
On Thu, Jun 12, 2003 at 03:12:32PM -0700, Austin Hastings wrote:
 
 sub Ficp400::SaveRow(Int $p_row)
 {
   return if IsDeleted($p_row);
 }


*laugh* Well, yes, there is always the obvious way.  I had wanted
something that would be reusable between multiple function, though
(sorry, should have said that explicitly).

I guess I got too caught up in the specific example; what I was really
curious about is if there was a way to skip up the stack on return.
And the answer, I see, is yes.  Thanks for pointing me at Cleave.

Yeesh.  Looks like I need to go back and reread A6 for a third time.
I obviously didn't understand it well enough.


--Dks