digitalmars.D.learn - cascade operator or nearest equivalent
- Daniel Davidson (32/32) Sep 12 2013 Is there any way to simulate the cascade operator (..) of Dart in
- bearophile (8/21) Sep 12 2013 Replacing Dart with D seems quite strange, such two languages
- Daniel Davidson (34/55) Oct 07 2013 The problem with with is the name can not hide each other, so it
Is there any way to simulate the cascade operator (..) of Dart in D? This operator makes fluid initialization simple. 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: 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', ]; So struct is a function that returns an instance of Struct. class Struct extends Decls { ... /// Documentation for this D struct String doc; /// List of members of this class List<Member> members = []; bool unitTest = false; ... } ..doc = ... assigns to the Struct.doc field and returns the original Struct instance so that ..unitTest = true can be applied to the original Struct instance, etc. What is the best way to achieve simple declarative style for very large initialization objects? Thanks, Dan
Sep 12 2013
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
Sep 12 2013
On Thursday, 12 September 2013 at 19:41:49 UTC, bearophile wrote:Daniel Davidson: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 DanI 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
Oct 07 2013