[swift-evolution] Implicit truncation

Xiaodi Wu xiaodi.wu at gmail.com
Mon May 22 09:35:49 CDT 2017


I should add: thanks for sharing real-world info on learning and teaching
Swift. I agree that it's important to consider how the language is picked
up by new users. Here, what I'm saying is that I think your learners have
missed a much larger pedagogical point. When I teach new learners, whether
it's C or Python or Swift, I introduce the notion that programming
languages treat integers specially, and that any time an operation produces
a fractional value in basic math, the integer math equivalent in a
programming language often drops the fractional part. I find that learners
who have had this introduction do not subsequently struggle with
float-to-int casting in C, for example.


On Mon, May 22, 2017 at 09:09 Xiaodi Wu <xiaodi.wu at gmail.com> wrote:

> Let's be clear: it _is_ rounded, just toward zero. It is consistent with
> the behavior of integer division. I would guess that your students also
> repeatedly struggle with the result that `2 / 3 == 0`? If so, they have not
> been taught some important fundamentals of integer arithmetic. If not, then
> it should be natural that `Int(2 / 3 as Double == Int(2 / 3 as UInt)`.
> On Mon, May 22, 2017 at 08:06 Rudolf Adamkovič <salutis at me.com> wrote:
>
>>
>> > On 22 May 2017, at 14:18, Xiaodi Wu <xiaodi.wu at gmail.com> wrote:
>> >
>> > What are the "subtle bugs all over the place" that you observe, and
>> what did those users expect to happen?
>>
>> They pretty much universally expect the value to be rounded. When this
>> happens, I tell them about this language called C (… insert long story here
>> ...) and we replace "Int(foo)" with "foo.rounded()”. We write a unit test
>> and move on. So far, so good. However, two weeks later, they do it again.
>> And again. If the initializer said “truncating” or something along those
>> lines, I’m sure they wouldn’t pick that one. They are all smart people,
>> it’s just that there’s absolutely no “visible clue” and they don’t expect
>> this to be the default behavior.
>>
>> P.S. I haven’t followed the “integer protocols” debate and had no idea
>> that “this issue is settled” and that it "cannot go back”. Also, I have no
>> time nor interest to "bikeshed endlessly”. I just wanted to share
>> real-world experience/observation with smart people in here (like you) to
>> improve the language going forward.
>>
>> Thank you!
>>
>> R+
>>
>>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20170522/42e9bf4d/attachment.html>


More information about the swift-evolution mailing list