次の方法で共有


"Length" in Avalon

Frank Hileman wonders about the Length type in a comment on a usability post to Steven Clarke's blog.  Here is his comment:

“..., like Length to represent a scalar. It is a scalar, why not use a double? This is what users want.“

Length is more than just the scalar value.  It also represents the units – thus Length allows specification in points, device independent pixels, centimeters, etc.  While one could imagine just taking doubles and having utlity functions to convert unit-based values into doubles, Length also allows for relative unit specification – for instance 62.5%.  This is not convertible by the user into a double value, thus a structure, like Length, is required to represent it.

Comments

  • Anonymous
    December 04, 2003
    Yes, I know this is why Length is used, and it maps perfectly onto the CSS and html tradition and object model. But consider the use in graphics. A user will want to specify Length in world coordinates. In that case a double is sufficient and preferable. So if there was an implicit conversion from double to a Length of type "independent pixel" (which I suppose is world coordinates) it would not be so clumsy. You do not have to allow an implicit conversion the other way. There is a conflict here between the flow layout typical use, and the absolute positioning typical use.
  • Anonymous
    December 05, 2003
    There are multiple drivers leading to this.

    One is that we want to provide a platform that unifies graphics, UI and documents as much as possible, and that sometimes leads to compromises in one area or another that benefits the whole. In the case of Length, we want to provide a single type at the Framework level.

    Regarding the providing of an implicit cast... this is something that is strongly discouraged in our design guidelines as, while it's conventient, can be misleading for users. The current set of internal guidelines (which will ultimately be published more broadly) is more definitive about this, but there's also reference to it in the currently published set:
    http://msdn.microsoft.com/library/en-us/cpgenref/html/cpconCasts.asp

    Finally, Length starts to appear at the "element" level of the system. Pure graphics-oriented applications may choose to use lower level access at the Visual level directly using the System.Windows.Media.DrawingContext to issue drawing instructions. All of these APIs take doubles directly that are interpreted as device independent pixels.
  • Anonymous
    December 05, 2003
    What about calling it "Unit"? Length is the number of elements in an array.
  • Anonymous
    December 05, 2003
    I think tend to agree that the name "Length" is potentially confusing - maybe "Size" would be clearer, indicating both a value and units.
  • Anonymous
    December 05, 2003
    Agreed that it can be confusing. However, we haven't seen better alternatives.

    For instance "Unit(4.0, UnitType.Points)" suggest that a "unit" is being create, but in fact, it's almost exactly the opposite -- an physical length measurement of something with a particular value and in a particular unit is created. The primary point of it is that it does represent a physical measure of length.

    Size is also confusing because there has been much precedent in the past for Size to mean a 2D size (width and height) and, in fact, in Avalon we use the Size type to denote just that as well.
  • Anonymous
    December 05, 2003
    The comment has been removed
  • Anonymous
    May 21, 2004
    We followed up on this over here, and it was pointed out that "domain" isn't really the only issue. It also has to do with whether the operations used on the "sub type" of the conversion (double in this case) need to be valid on the "super type" of the conversion (Length in this case).

    For double/Length, this isn't the case, as +, for instance, isn't defined on Length, nor should it be, since adding Lengths of different unit types doesn't make sense.

    So, I agree, it's somewhat more painful than one would like it to be, but our feeling is that that's better than the confusion it would cause by not having a properly applied implicit conversion.
  • Anonymous
    June 03, 2004
    Allow +, -, etc, and throw an exception if the units are incompatible.