cascade operator or nearest equivalent

Daniel Davidson nospam at spam.com
Mon Oct 7 17:16:18 PDT 2013


On Thursday, 12 September 2013 at 19:41:49 UTC, bearophile wrote:
> Daniel Davidson:
>
>> I am using Dart for code generation but would like to consider 
>> D if I can find a convenient replacement for the following 
>> declarative style:
>
> Replacing Dart with D seems quite strange, such two languages 
> have so much different usage niches.
>
>
>>    var dateRange = struct('date_range')
>>      ..doc = 'Basic pair of start and end dates'
>>      ..unitTest = true
>>      ..publicSection = true
>>      ..members = [
>>        member('start_date')
>>        ..type = 'Date',
>>        member('end_date')
>>        ..type = 'Date',
>>      ];
>
> A similar syntax is not allowed in D, but there are two things 
> that help for this: the C-style initialization of structs, that 
> support field names too, and the with(){} statement.
>
> Bye,
> bearophile

The problem with with is the name can not hide each other, so it 
is easy to get into trouble. With C-style initialization I don't 
think you can use as expression - I think you need an lvalue for 
each and this hurts nesting.

I have a style I'm playing with and if anyone has comments or 
improvements (i.e. make more succinct/readable) it would be 
appreciated.

Here is a sample and the source for this and comparable Dart are 
in the links.

     auto d = make((ref Dossier _) {
       _.family = [
        "father" : make((ref Person _) {
            _.birthDate = "2001/1/1";
            _.deathDate = "2101/1/1";
            _.retirementDate = "2100/1/1"; }),
        "mother" : make((ref Person _) {
            _.birthDate = "2005/1/1";
            _.deathDate = "2125/1/1";
            _.retirementDate = "2100/1/1"; }),
        ];
       _.assets = [
        "house" : make((ref Asset _) {
            _.name = "Home on the Hill";
            _.unitValue = 120_000; }),
        "car" : make((ref Asset _) {
            _.name = "Dodge Dart";
            _.unitValue = 500; })
       ];
     });

http://pastebin.com/iLVL20Bz
http://pastebin.com/mLcWDACm

Thanks
Dan


More information about the Digitalmars-d-learn mailing list