Skip to content

Default Values in a func are not working for func-parameters or to satisfy protocols #82521

Open
@cvogelbusch

Description

@cvogelbusch

Description

A default parameter in a functions signature should mean this Parameter can be omitted. Sadly that only works in some situations which makes no sense for me - in my head this definition means that this function has multiple signatures kind of.

I found two cases where it does not work. When using the function as a parameter or when using it to satisfy a protocol.

Reproduction

import Foundation

struct TestA {
    var one: Int
    var two: Int
    
    init(one: Int, two: Int = 2) {
        self.one = one
        self.two = two
    }
    
    func needs(this: Int, and that: Int = 2) {
        print("This \(this), that \(that)")
    }
}

struct TestB {
    var one: Int
    var two: Int
    
    init(one: Int) {
        self.one = one
        self.two = 2
    }
    
    func needs(this: Int) {
        print("This \(this), that \(2)")
    }
}

let testA = (1...10).map(TestA.init)
let testB = (1...10).map(TestB.init)

protocol TestProtocol {
    func needs(this: Int)
}

extension TestA: TestProtocol {}
extension TestB: TestProtocol {}

//extension TestA {
//    func needs(this: Int) {
//        print("This \(this)")
//    }
//    init (one: Int) {
//        self.one = one
//        self.two = 0
//    }
//}

Expected behavior

I would expect that both TestA and TestB can do the mapping, because they their init can both be called with a single parameter of type Int.
Also I would assume TestA and TestB satisfies TestProtocol, because the function can be called as requested.

Actual Result:
Both do not "see" the function signature they need to call it with a single parameter
Line 31: Cannot convert value of type '@sendable (Int, Int) -> TestA' to expected argument type '(ClosedRange.Element) -> TestA' (aka '(Int) -> TestA')
Line 38: Type 'TestA' does not conform to protocol 'TestProtocol'

What is even weirder to me is I can declare the init and function with a single parameter (like in the comment), but not I do not know which one is called when I call the function with a single parameter in my code.

Environment

swift-driver version: 1.120.5 Apple Swift version 6.1.2 (swiftlang-6.1.2.1.2 clang-1700.0.13.5)
Target: arm64-apple-macosx15.0

Additional information

I really appreciate all you people working on Swift it is a lovely language and for no other language I would expect this to work, but Swift feels so clean to me that this should be working.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugA deviation from expected or documented behavior. Also: expected but undesirable behavior.triage neededThis issue needs more specific labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions