[swift-evolution] [Pitch] Percentage Type
Saagar Jha
saagar at saagarjha.com
Wed Jan 17 19:36:45 CST 2018
Saagar Jha
> On Jan 17, 2018, at 16:56, Xiaodi Wu via swift-evolution <swift-evolution at swift.org> wrote:
>
> On Wed, Jan 17, 2018 at 2:04 AM, David Sweeris <davesweeris at mac.com <mailto: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 <mailto: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?
Just a thought: if Swift ever allows integer literals in generics (e.g. for a fixed size array equivalent to C++’s std::array), this seems like how a type like ScaleFrom (and a percent type, for that matter) could be implemented.
>
> 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.
> _______________________________________________
> swift-evolution mailing list
> swift-evolution at swift.org <mailto:swift-evolution at swift.org>
> https://lists.swift.org/mailman/listinfo/swift-evolution <https://lists.swift.org/mailman/listinfo/swift-evolution>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20180117/c3a5e3e6/attachment.html>
More information about the swift-evolution
mailing list