Skip to content

Default parameter is not leveraged when the variable that is assigned the function with the default argument is called #65092

Open
@andrewkykoo

Description

@andrewkykoo

Description

  1. There is a function that takes a default value for a parameter
  2. Let's define a variable as a function type by assigning the function above to the variable
  3. When the variable is called without a parameter, it does not leverage the default parameter

Steps to reproduce

func showNumber(num: Int = 10) {
    print(num)
}

var showNum = showNumber

showNum()    // throws an error ("Missing argument for parameter #1 in call")
// modified to take an optional Int? argument
func showNumber(num: Int? = 10) {
    if let num = num {
        print(num)
    } else {
        print("num is nil")
    }

...

showNum()    // still throws the same error

Expected behavior

  • Expected showNum() to print 10

Environment

  • Swift compiler version info : swift-driver version: 1.75.2 Apple Swift version 5.8 (swiftlang-5.8.0.124.2 clang-1403.0.22.11.100) Target: arm64-apple-macosx13.0
  • Xcode version info : Xcode 14.3 Build version 14E222b
  • Deployment target: : macOS

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugA deviation from expected or documented behavior. Also: expected but undesirable behavior.compilerThe Swift compiler itselfdefault argumentsFeature: default arguments for value parametersexpressionsFeature: expressionsfunction parametersFeature → declarations: function parametersfunction typesFeature → types: function typesfunction valuesFeature → expressions: function valuesnot a bugResolution → not a bug: Reported as a bug but turned out to be expected behavior or programmer errorswift 5.8type checkerArea → compiler: Semantic analysis

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions