[swift-evolution] [Pitch] Percentage Type

Xiaodi Wu xiaodi.wu at gmail.com
Wed Jan 17 18:55:41 CST 2018


On Wed, Jan 17, 2018 at 2:04 AM, David Sweeris <davesweeris at mac.com> wrote:

>
>
> Sent from my iPhone
>
> > On Jan 16, 2018, at 23:45, Jonathan Hull via swift-evolution <
> swift-evolution at swift.org> wrote:
> >
> > Mainly semantics.
> >
> > We could technically use Int instead of having a Bool type (just using 1
> and 0).  We don’t do that since Int and Bool have intrinsically different
> meanings in code.
> >
> > What I am saying is that parameters that take the range 0 to 1 typically
> have a fundamentally different meaning (or at least a different way of
> thinking about them) than Doubles.  It would be nice to be able to see that
> distinction when using APIs.
> >
> > With both this and the Angle type, I am pointing out areas where, due to
> historical reasons in C, we have conflated a bunch of types which have
> different behavior, and then just expect programmers to be conscientious
> enough to use them correctly in each case.  These types/numbers all have a
> different forms of dimensionality.
> >
> > I’d like to discuss that before we lock everything down.
>
> +1 (although I think a “normalized to [0, 1]” type would be more useful
> than a “percentage” type)
>

Bool is not a good example; it permits precisely two logical values (0 and
1). By contrast, if you're going to support 1000%, then your type supports
the same values as the underlying storage. As I wrote in a different
thread, one way to look at a type is the set of values that a variable can
have.

What is your limiting principle here if you think that a range that's not
enforced makes a value become of a different type? Often, a 1-5 rating
system is used. Sometimes, it's 1-4 or 1-10. And of course, a "3" on a 1-5
scale means something very different from a "3" on a 1-10 scale. Should
ScaleFrom1To5 be its own type? And also ScaleFrom1To4 and ScaleFrom1To10?

Besides, even supposing a percentage type would be in high demand, there's
no need for its inclusion in the standard library. It's very easy to
implement on your own in a third-party library. Moreover, custom operators
will allow you to define a postfix `%`, and then you could write: `let x =
100%`. Throw in some heterogeneous arithmetic operators and you could do
almost any math you want.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20180117/40bbada0/attachment.html>


More information about the swift-evolution mailing list