-1

I was playing around with an extension which I noticed Xcode does not know or it cannot infer round and I have to use like SwiftUI.round for helping Xcode! why it is the case? I think I do not have to use SwiftUI. because round function is a public function. So what happens there? and how can I use just round instead of SwiftUI.round?

Xcode Version 13.4.1 (13F100)

import SwiftUI

struct ContentView: View {
    var body: some View {
        Text("Hello, world!")
            .onAppear {
                let value: Double = 2.69
                print(value.string)
            }
        
    }
}

extension Double {
    var string: String {
        let value: Self = SwiftUI.round(self*10.0)/10.0
        return String(describing: value)
    }
}

The issue is here that the round function is public as Xcode say:

public func round(_: Double) -> Double

So if I create a custom function called:

public func test(_ value: Double) -> String {
    return String(describing: value)
}

I would be able to call test function just with test(...) so why this is not possible with other public function called round(_: Double)

ios coder
  • 1
  • 4
  • 31
  • 91
  • 1
    `(self*10.0).rounded()/10.0`, see https://stackoverflow.com/a/38036978/1187415 – Martin R Aug 24 '22 at 10:19
  • Thanks, I want to know why Xcode cannot infer it, I do not have issue for rounding. Because when I import SwiftUI, it must be accessible inside extension as well. – ios coder Aug 24 '22 at 10:24

1 Answers1

2

Double has the method

mutating func round(_ rule: FloatingPointRoundingRule)

therefore, inside an extension of that type, round() without explicit module name is inferred as this mutating method. As an example, this would compile:

extension Double {
    var string: String {
        var x = self * 10.0
        x.round()
        let value = x/10.0
        return String(describing: value)
    }
}

You can use Darwin.round() or (apparently) SwiftUI.round() to refer explicitly to the C library function.

Alternatively, use the rounded() method.

Martin R
  • 529,903
  • 94
  • 1,240
  • 1,382
  • I am not talking about how I can round a value, this is not the question, also I am not talking to `.round()` or `FloatingPointRoundingRule` I am talking about `public func round(_: Double) -> Double` and why the public function is not accessible in extension. I do not need codes, I need explaining about the issue. – ios coder Aug 24 '22 at 10:42
  • @ioscoder: And I tried to explain it. There is a global `round()` function and a `Double.round()` method. Inside an extension of the Double type, the compiler infers “round” as the `Double.round()` method, and the global function is only accessible with explicit module name.. – Martin R Aug 24 '22 at 10:43
  • Oh, I can see now! So Xcode infer the things it thinks it is the correct one, because we are inside Double extension, but in the fact it ignores the public function we are trying to use, right? – ios coder Aug 24 '22 at 10:46
  • 1
    @ioscoder: Inside a Double extension, “round” is always interpreted as the `Double.round()` method, and not as the global function. – A similar example: Inside an Array extension, “min” is understood as the `Array.min()` method and not as the global `min()` function. That has been treated elsewhere on this site, I think. – That's just how it is. The compiler *could* do better, but it has not been implemented. – Martin R Aug 24 '22 at 10:54