Company logo
  • Jobs
  • Bootcamp
  • About Us
  • For professionals
    • Home
    • Jobs
    • Courses
    • Questions
    • Teachers
    • Bootcamp
  • For business
    • Home
    • Our process
    • Plans
    • Assessments
    • Payroll
    • Blog
    • Calculator

0

21
Views
Negative integer literal converted to generic unsigned integer type without warning

In this simple piece of code, I'm using the integer literal -1 in a context where a value of type T is expected. T conforms to FixedWidthInteger so it's unknown at compile time whether the literal can be converted to the actual type T. If T is set to an unsigned integer type, the -1 simply becomes a 0. I would have expected a runtime error, or at least a warning at compile time. Is this a bug or is this documented somewhere?

struct Bad<T: FixedWidthInteger> {
    func getNegativeOne() -> T {
        return -1
    }
}

print(Bad<UInt32>().getNegativeOne())

When running it from the swift repl:

$ swift
Welcome to Swift version 5.5.2-dev.
Type :help for assistance.
  1> [paste code]
0
10 months ago · Santiago Trujillo
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post job Plans Our process Sales
Legal
Terms and conditions Privacy policy
© 2023 PeakU Inc. All Rights Reserved.