Posted By: dcuccia | Sep 10th, 2008 @ 11:53 PM
page 1 of 1
Comments: 4 | Views: 956
I'm playing around with trying to make array math in C# as easy and concise as in Matlab, and (besides array operators) the biggest language "mismatch" is in indexers, ranges, etc. In Matlab, I can easily get a subarray of A, such as A[0:5:255]

As a user of a fluent interface DSL in C#, would you ever put up with unconventional use of indexers? Like:

A[Get.Range[0,5,255]]


...where Range is actually a static readonly field holding an instance of class Range

    public class Get<br>    {<br>        public static readonly Range Range = new Range();<br>        public static readonly Size Size = new Size();<br>    }<br><br>    public class Range<br>    {<br>        public Range this[int from, int to] { get { return new Range(from, 1, to); } }<br>        public Range this[int from, int every, int to] { get { return new Range(from, every, to); } }<br><br>        private Range(int start, int every, int stop) { Start = start; Every = every;  Stop = stop; }<br>        private Range(int start, int stop) : this(start, 1, stop) { }<br>        internal Range() { } // for indexer use<br><br>        public int Start { get; private set; }<br>        public int Stop { get; private set; }<br>        public int Every { get; private set; }<br>        public int Count() { return (Stop - Start) / Every + 1; }<br>    }<br><br>    public class Size<br>    {<br>        public Size this[int d0] { get { return new Size(new int[] { d0 }); } }<br>        public Size this[int d0, int d1] { get { return new Size(new int[] { d0, d1 }); } }<br>        public Size this[int d0, int d1, int d2] { get { return new Size(new int[] { d0, d1, d2 }); } }<br>        public Size this[int d0, int d1, int d2, int d3] { get { return new Size(new int[] { d0, d1, d2, d3 }); } }<br><br>        private Size(int[] dimensions) { Dimensions = dimensions.ToArray(); }<br>        internal Size() { } // for indexer use<br><br>        public int[] Dimensions { get; private set; }<br>    }<br>


Obviously the following would be more C-sharpy:

A[new Range(0,5,255)]


But, isn't it OK to stray a bit as long as there's a consistent convention in the DSL?

you may also want to make  the constructor of the Range class internal, to force the user to use it via Get class (if you use first approach), I would suggest to use the second one, but the first one looks cooler...
stevo_
stevo_
Human after all
I'd be honest and say I don't think it adds anything or makes it really different, to me it just serves as a barrier for people to realize whats actually going on here..
I would prefer A.Range(from, to) and A.Range(from, to, every) rather than the indexer, but that's just my opinion.
page 1 of 1
Comments: 4 | Views: 956
Microsoft Communities