new Address()
..street = (new StreetAddress()
..number = 123
..street = (new Street()
..name = "Main"
..kind = "St."))
..city = "Springville"
..state = "Illinois";
That's the problem with cascade: it doesn't look good with nesting. It's
hard to write, hard to read, and generally weird.
I've given it a bit of philosophical thought over the years :)
Here is the summary of my findings so far:
Suppose you are writing a function, and it gets complicated, with nested
ifs, for(s) etc. Or just the body is too long - you get out of breath
trying to read it.
What do you do?
Sure, you define a bunch of simpler functions, with supposedly meaningful
names, and refactor the big one.
What stops you from doing the same wrt structured data as above?
E.g. you could write
var street= new Street()
..name= "Main"
..kind= "St.";
var streetAddress=new StreetAddress()
..number= 123
..street= street;
var address = new Address()
..streetAddress = streetAddress,
..city = "Springville"
..state = "Illinois";
It is much easier to read, but here's the problem: if you need more than
one address, you cannot reuse variables "street" and "streetAddress" - you
need "street1" and "streetAddress1" etc - which is ugly and error prone.
What to do? By the way, things don't get much different with any other data
notation. With JSON, you can write 1 more level of nesting before it gets
incomprehensible, but no more than that. With XML - maybe you can afford 2
extra levels, but the thing already looks monumental and a bit of out of
place in a programming language.
I'm afraid generated constructor will not get you past level 2 anyway.
Whether a good solution exists, I don't know. Maybe if we had "blocks
returning a value" (we discussed it in another thread) we could achieve
the same result, but at least those blocks can be used also for
initialization of variables (so the feature is not as ad-hoc as generated
constructor).
I find the syntax (){ ... return 1;}() atrocious, so maybe special kind of
"auto-executed block" can be introduced, like
var x=${
//any code that uses return statements
}
Then you could write it like this:
var address = ${
var street= new Street()
..name= "Main"
..kind= "St.";
var streetAddress=new StreetAddress()
..number= 123
..street= street;
return new Address()
..streetAddress = streetAddress,
..city = "Springville"
..state = "Illinois";
}
How about that? (BTW, this seems to be backward compatible, no?)
--
For other discussions, see https://groups.google.com/a/dartlang.org/
For HOWTO questions, visit http://stackoverflow.com/tags/dart
To file a bug report or feature request, go to http://www.dartbug.com/new
To unsubscribe from this group and stop receiving emails from it, send an email to misc+***@dartlang.org.