<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://blogs.msdn.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Some good feedback on the Range post.</title><link>http://blogs.msdn.com/jaybaz_ms/archive/2004/08/20/217789.aspx</link><description>Dithermaster says “it's *much* easier to find out of they DON'T overlap” and proposes: ! ( (end2 &amp;lt; start1) || (start2 &amp;gt; end1) ); If we apply DeMorgan’s Law, we get: (! (end2 &amp;lt; start1) &amp;amp;&amp;amp; !(start2 &amp;gt; end1) ); And (end2 &amp;gt;= start1)</description><dc:language>en-US</dc:language><generator>CommunityServer 2.1 SP1 (Build: 61025.2)</generator><item><title>re: Some good feedback on the Range post.</title><link>http://blogs.msdn.com/jaybaz_ms/archive/2004/08/20/217789.aspx#217883</link><pubDate>Fri, 20 Aug 2004 18:46:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:217883</guid><dc:creator>James Manning</dc:creator><description>Operator constraints (and method constraints in general) would be nice to have.  It reminds me of the IArithmetic proposal that was on OSNews a couple weeks back.&lt;br&gt;&lt;br&gt;&lt;a target="_new" href="http://www.osnews.com/story.php?news_id=7930"&gt;http://www.osnews.com/story.php?news_id=7930&lt;/a&gt;&lt;br&gt;&lt;br&gt;</description></item><item><title>re: Some good feedback on the Range post.</title><link>http://blogs.msdn.com/jaybaz_ms/archive/2004/08/20/217789.aspx#217906</link><pubDate>Fri, 20 Aug 2004 19:30:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:217906</guid><dc:creator>Matthew W. Jackson</dc:creator><description>For compairison operations, I'm thinking abuot writing a Comparable&amp;lt;T&amp;gt; struct which wraps an IComparable&amp;lt;T&amp;gt; value and implements all of the needed operators (&amp;lt;, &amp;gt;, &amp;lt;=, &amp;gt;=, !=, ==).  Since it's a struct, it won't add any memory overhead, and all of the methods should be short, and being non-virtual, they should be inlined by the JIT.&lt;br&gt;&lt;br&gt;Any time I would need to store a Comparable value, I could use this instead of the actual value.&lt;br&gt;&lt;br&gt;This might make code cleaner, but it also might make it more confusing by adding another layer.  I'll have to try it out next chance I get.&lt;br&gt;&lt;br&gt;I'm trying to decide if it should have an implict conversion operator to its underlying type.  That way, I could store a Comparable&amp;lt;T&amp;gt; and return it in a function that just returns T.&lt;br&gt;&lt;br&gt;Anyone have some thoughts on the pros and cons of this approach?</description></item><item><title>re: Some good feedback on the Range post.</title><link>http://blogs.msdn.com/jaybaz_ms/archive/2004/08/20/217789.aspx#217912</link><pubDate>Fri, 20 Aug 2004 19:39:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:217912</guid><dc:creator>jaybaz [MS]</dc:creator><description>Mattew, I'm curious where you're going with this.  I tried playing with some stuff, but didn't get anywhere.  Can you put together an example that demonstrates the idea?</description></item><item><title>re: Some good feedback on the Range post.</title><link>http://blogs.msdn.com/jaybaz_ms/archive/2004/08/20/217789.aspx#218089</link><pubDate>Sat, 21 Aug 2004 02:26:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:218089</guid><dc:creator>Matthew W. Jackson</dc:creator><description>  // Comparable&amp;lt;T&amp;gt; is a semi-transparent struct for adding comparison operators&lt;br&gt;  // to the IComparable&amp;lt;T&amp;gt; interface.  It can be implicitly converted to and from&lt;br&gt;  // T, and therefore can be compared to T with any of the comparison operators.&lt;br&gt;  [CLSCompliant(false)]&lt;br&gt;  public struct Comparable&amp;lt;T&amp;gt; : IComparable&amp;lt;Comparable&amp;lt;T&amp;gt;&amp;gt;, IComparable &lt;br&gt;    where T : IComparable&amp;lt;T&amp;gt; {&lt;br&gt;&lt;br&gt;    private T value;&lt;br&gt;&lt;br&gt;    public Comparable(T value) {&lt;br&gt;      this.value = value;&lt;br&gt;    }&lt;br&gt;&lt;br&gt;    public T Value {&lt;br&gt;      get { return this.value; }&lt;br&gt;    }&lt;br&gt;&lt;br&gt;    public bool Equals(Comparable&amp;lt;T&amp;gt; other) {&lt;br&gt;      return this.value.Equals(other.value);&lt;br&gt;    }&lt;br&gt;&lt;br&gt;    public int CompareTo(Comparable&amp;lt;T&amp;gt; other) {&lt;br&gt;      return this.value.CompareTo(other.value);&lt;br&gt;    }&lt;br&gt;&lt;br&gt;    public int CompareTo(object obj) {&lt;br&gt;      if(obj is Comparable&amp;lt;T&amp;gt;)&lt;br&gt;        return this.CompareTo((Comparable&amp;lt;T&amp;gt;)obj);&lt;br&gt;      throw new ArgumentException(&amp;quot;Invalid argument type&amp;quot;, &amp;quot;obj&amp;quot;);&lt;br&gt;    }&lt;br&gt;&lt;br&gt;    public override int GetHashCode() {&lt;br&gt;      return this.value.GetHashCode();&lt;br&gt;    }&lt;br&gt;&lt;br&gt;    public override bool Equals(object obj) {&lt;br&gt;      if(obj is Comparable&amp;lt;T&amp;gt;)&lt;br&gt;        return this.Equals((Comparable&amp;lt;T&amp;gt;)obj);&lt;br&gt;      return false;&lt;br&gt;    }&lt;br&gt;&lt;br&gt;    public override string ToString() {&lt;br&gt;      return this.value.ToString();&lt;br&gt;    }&lt;br&gt;&lt;br&gt;    public static bool operator ==(Comparable&amp;lt;T&amp;gt; comparableValue1, Comparable&amp;lt;T&amp;gt; comparableValue2) {&lt;br&gt;      return comparableValue1.Equals(comparableValue2);&lt;br&gt;    }&lt;br&gt;&lt;br&gt;    public static bool operator !=(Comparable&amp;lt;T&amp;gt; comparableValue1, Comparable&amp;lt;T&amp;gt; comparableValue2) {&lt;br&gt;      return !comparableValue1.Equals(comparableValue2);&lt;br&gt;    }&lt;br&gt;&lt;br&gt;    public static bool operator &amp;gt;(Comparable&amp;lt;T&amp;gt; comparableValue1, Comparable&amp;lt;T&amp;gt; comparableValue2) {&lt;br&gt;      return (comparableValue1.CompareTo(comparableValue2) &amp;gt; 0);&lt;br&gt;    }&lt;br&gt;&lt;br&gt;    public static bool operator &amp;lt;(Comparable&amp;lt;T&amp;gt; comparableValue1, Comparable&amp;lt;T&amp;gt; comparableValue2) {&lt;br&gt;      return (comparableValue1.CompareTo(comparableValue2) &amp;lt; 0);&lt;br&gt;    }&lt;br&gt;&lt;br&gt;    public static bool operator &amp;gt;=(Comparable&amp;lt;T&amp;gt; comparableValue1, Comparable&amp;lt;T&amp;gt; comparableValue2) {&lt;br&gt;      return (comparableValue1.CompareTo(comparableValue2) &amp;gt;= 0);&lt;br&gt;    }&lt;br&gt;&lt;br&gt;    public static bool operator &amp;lt;=(Comparable&amp;lt;T&amp;gt; comparableValue1, Comparable&amp;lt;T&amp;gt; comparableValue2) {&lt;br&gt;      return comparableValue1.CompareTo(comparableValue2) &amp;lt;= 0;&lt;br&gt;    }&lt;br&gt;&lt;br&gt;    public static implicit operator T(Comparable&amp;lt;T&amp;gt; comparableValue) {&lt;br&gt;      return comparableValue.value;&lt;br&gt;    }&lt;br&gt;&lt;br&gt;    public static implicit operator Comparable&amp;lt;T&amp;gt;(T value) {&lt;br&gt;      return new Comparable&amp;lt;T&amp;gt;(value);&lt;br&gt;    }&lt;br&gt;&lt;br&gt;  }&lt;br&gt;&lt;br&gt;  // Subset of a Range struct (could also be a class) that utilizes the Comparable&amp;lt;T&amp;gt;&lt;br&gt;  // struct.  Notice the contains method, which uses normal comparison operators instead&lt;br&gt;  // of complicated CompareTo(T) calls.&lt;br&gt;  [CLSCompliant(false)]&lt;br&gt;  public struct Range&amp;lt;T&amp;gt; where T : IComparable&amp;lt;T&amp;gt; {&lt;br&gt;&lt;br&gt;    private Comparable&amp;lt;T&amp;gt; start;&lt;br&gt;    private Comparable&amp;lt;T&amp;gt; end;&lt;br&gt;&lt;br&gt;    public Range(T start, T end) {&lt;br&gt;      this.start = start;&lt;br&gt;      this.end = end;&lt;br&gt;    }&lt;br&gt;&lt;br&gt;    public T Start {&lt;br&gt;      get { return this.start; }&lt;br&gt;    }&lt;br&gt;&lt;br&gt;    public T End {&lt;br&gt;      get { return this.end; }&lt;br&gt;    }&lt;br&gt;&lt;br&gt;    public bool Contains(T other) {&lt;br&gt;      return (start &amp;lt;= other) &amp;amp;&amp;amp; (end &amp;gt;= other);&lt;br&gt;    }&lt;br&gt;&lt;br&gt;  }</description></item><item><title>re: Some good feedback on the Range post.</title><link>http://blogs.msdn.com/jaybaz_ms/archive/2004/08/20/217789.aspx#218144</link><pubDate>Sat, 21 Aug 2004 07:45:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:218144</guid><dc:creator>Matthew W. Jackson</dc:creator><description>By the way, if the Framework designers decide to include some form of IArithmetic&amp;lt;T&amp;gt; (and I really really really really really really really really hope they do, as it can solve a bunch of generic problems without changing the current languages--ie no immediate need for operator constaints), then I would probably design a similar pattern around that interface as well.&lt;br&gt;&lt;br&gt;I'd probably call it struct Number&amp;lt;T&amp;gt; where T : IArithmetic&amp;lt;T&amp;gt;.&lt;br&gt;&lt;br&gt;I'd implement all of the operators that make sense for numbers, and then any time I write a generic class that needs to work with numbers, I work with Number&amp;lt;T&amp;gt; instead of T itself, since &amp;quot;value1 + value2&amp;quot; reads much much better than &amp;quot;value1.AddTo(value2)&amp;quot;.&lt;br&gt;&lt;br&gt;But this whole concept is moot if IArithmetic&amp;lt;T&amp;gt; (or equivalent) is left out of the BCL.&lt;br&gt;&lt;br&gt;By the way, I'm really liking the implementation of Comparable&amp;lt;T&amp;gt;.  I'm still not sure if the extra layer makes what's happening in the code less clear, but I really enjoy not having to call CompareTo(T).</description></item><item><title>re: Some good feedback on the Range post.</title><link>http://blogs.msdn.com/jaybaz_ms/archive/2004/08/20/217789.aspx#218258</link><pubDate>Sat, 21 Aug 2004 19:36:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:218258</guid><dc:creator>jaybaz [MS]</dc:creator><description>The Comparable&amp;lt;T&amp;gt; is interesting because it's something the consumer applies, instead of being a feature of the comparable class.  It does seem to work quite nicely.&lt;br&gt;&lt;br&gt;You used a struct.  That's probably just fine, but I think it's important that you make fields of a struct 'readonly', as mutable structs often have surprising semantics.&lt;br&gt;&lt;br&gt;I'd also choose to write 3 of the operators via their complements.  At least for me, that means I'm more likely to get it right.&lt;br&gt;&lt;br&gt;    public static bool operator !=(Comparable&amp;lt;T&amp;gt; comparableValue1, Comparable&amp;lt;T&amp;gt; comparableValue2)&lt;br&gt;    {&lt;br&gt;        return !(comparableValue1 == comparableValue2);&lt;br&gt;    }&lt;br&gt;</description></item><item><title>re: Some good feedback on the Range post.</title><link>http://blogs.msdn.com/jaybaz_ms/archive/2004/08/20/217789.aspx#218315</link><pubDate>Sat, 21 Aug 2004 21:33:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:218315</guid><dc:creator>Matthew W. Jackson</dc:creator><description>I hope I didn't just flood the comments with posts.  None of my responses seem to be going through.</description></item><item><title>re: Some good feedback on the Range post.</title><link>http://blogs.msdn.com/jaybaz_ms/archive/2004/08/20/217789.aspx#218318</link><pubDate>Sat, 21 Aug 2004 21:36:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:218318</guid><dc:creator>Matthew W. Jackson</dc:creator><description>You are right about mutable structs.  I detest them. I do not like System.Drawing.Rectangle for that reason. There are a few exceptions, such as caching a hash code or something, but that does not really change the value. And yeah, I forgot to mark my fields as readonly, but the struct is still (publicly, at least) immutable.  Thanks for pointing that out...it is now fixed in my code. Anyway, I am a big fan of structs when it comes to values that need value semmantics.  In this case I used a struct to keep from allocating another heap object just because I wanted to add operators to an interface. Not to mention the performance gains when using structs in generics.</description></item><item><title>re: Some good feedback on the Range post.</title><link>http://blogs.msdn.com/jaybaz_ms/archive/2004/08/20/217789.aspx#218324</link><pubDate>Sat, 21 Aug 2004 21:42:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:218324</guid><dc:creator>Matthew W. Jackson</dc:creator><description>As for the operators, that is all a matter of taste. I try to minimize the amount of layers in the logic because I tend to like my structs to be performant, but I still do not want to implement the core logic in more than one place, so I am happy with making the operators call the named static methods.  Also, it keeps the methods somewhat balanced: == calls Equals and != calls !Equals, instead of == calls Equals and != calls == calls Equals. The other solution is to make the named static methods call the operators, but that always confuses me. I do not really like having a type use its own operators.  That is also why I would not implement != to call ==.  But as long as the code is correct, I could not care less how other people do it.</description></item><item><title>re: Some good feedback on the Range post.</title><link>http://blogs.msdn.com/jaybaz_ms/archive/2004/08/20/217789.aspx#218325</link><pubDate>Sat, 21 Aug 2004 21:51:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:218325</guid><dc:creator>jaybaz [MS]</dc:creator><description>Matthew: No spam that I see.&lt;br&gt;&lt;br&gt;If C# were my language, I would add the partially-reserved word 'mutable' as a modifier on structs:&lt;br&gt;&lt;br&gt;  mutable struct S { readonly int x; } // warning: no mutable members&lt;br&gt;&lt;br&gt;  struct S { int x; } // error: mutable struct not marked as such&lt;br&gt;&lt;br&gt;  mutable struct S { int x; } // OK&lt;br&gt;&lt;br&gt;  struct S { readonly int x; } // OK&lt;br&gt;&lt;br&gt;then I would go through the .Net FX and fix up all the structs to work correctly; ideally make them all immutable.</description></item><item><title>re: Some good feedback on the Range post.</title><link>http://blogs.msdn.com/jaybaz_ms/archive/2004/08/20/217789.aspx#218474</link><pubDate>Sun, 22 Aug 2004 03:45:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:218474</guid><dc:creator>Matthew W. Jackson</dc:creator><description>It seems like the kind of thing FxCop would be useful for.</description></item><item><title>re: Some good feedback on the Range post.</title><link>http://blogs.msdn.com/jaybaz_ms/archive/2004/08/20/217789.aspx#219511</link><pubDate>Tue, 24 Aug 2004 15:12:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:219511</guid><dc:creator>Matthew W. Jackson</dc:creator><description>I just stumbled upon an earlier blog of yours where you talk about using FxCop for this.&lt;br&gt;&lt;br&gt;Touch&amp;#233;.</description></item><item><title>re: Some good feedback on the Range post.</title><link>http://blogs.msdn.com/jaybaz_ms/archive/2004/08/20/217789.aspx#219738</link><pubDate>Tue, 24 Aug 2004 20:48:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:219738</guid><dc:creator>jaybaz [MS]</dc:creator><description>Matthew: While it could be done in FxCop, I actually think that immutability of structs should be a first-class feature of the language.</description></item><item><title>re: Some good feedback on the Range post.</title><link>http://blogs.msdn.com/jaybaz_ms/archive/2004/08/20/217789.aspx#219935</link><pubDate>Wed, 25 Aug 2004 02:36:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:219935</guid><dc:creator>Matthew W. Jackson</dc:creator><description>Seems like there are new &amp;quot;mistakes&amp;quot; in Whidbey regarding structs.  I think that the mutable design of KeyValuePair&amp;lt;K, V&amp;gt; is an incorrect design, but since there are no mutating methods, it shouldn't cause any direct problems.  Still, I consider a Pair to be a value, not a collection of values, and therefore changing one of the members seems wrong.  I personally think they should have made Key and Value readonly.  The nicest thing about keeping structs immutable is that they follow almost the same semantics of immutable classes, except for the existance of null values.&lt;br&gt;&lt;br&gt;I don't know if it would be worth submitting a suggestion on MSDN about this.  It seems that there seems to be a big split on the issue.</description></item><item><title>Building a generic Range class</title><link>http://blogs.msdn.com/jaybaz_ms/archive/2004/08/20/217789.aspx#3010836</link><pubDate>Thu, 31 May 2007 20:41:15 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:3010836</guid><dc:creator>Coding Sanity</dc:creator><description>&lt;p&gt;Quite a long article where I discuss the creation of a generic Range class, and go into the decisions and thinking about many of the design aspects. Touches on lambdas, iterators, generics, and the usual rambling grab-bag of stuff I go on about when I&lt;/p&gt;
</description></item></channel></rss>