Friday, 2 August 2013

Get 'With' It Dart! One Thing Dart Could Learn From VB.net

UPDATE : Turns out the cascade operator does work as I expect - my syntax was a bit off as only the final semi-colon is required! This works today - yippee! :

  crc
    ..beginPath()
    ..moveTo(ax,ay);

Thanks to those who provided me with such rapid feedback!

Dart has a great feature - the cascade operator - that helps construct a new or returned configured object fast.

query('#button')
    ..text = 'Ok'
    ..classes.add('Bold')

But oddly it doesn't seem to work if you have an existing reference:

  crc
    ..beginPath();
    ..moveTo(ax,ay);

This will give an error.

Consider the following Dart code snippet:

  crc.beginPath();  
  crc.moveTo(ax,ay);  
  crc.lineTo(bx,by);  
  crc.lineTo(cx,cy);  
  crc.fill(); 

It would nice to have the cascade operator work this way or have a With ... End With statement (from VB.net) to use (I'll use curly brackets so it is C style) like so:

  with crc
  {
    .beginPath();  
    .moveTo(ax,ay);  
    .lineTo(bx,by);  
    .lineTo(cx,cy);  
    .fill();
  }
OR
  crc
    ..beginPath();  
    ..moveTo(ax,ay);  
    ..lineTo(bx,by);  
    ..lineTo(cx,cy);  
    ..fill();

Before I raise a feature request, I'd love to hear what you think. It is a nice to have but I think it helps make it clearer and more consistent. Are you with me? :-)