Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
andrewkykoo opened this issue Apr 12, 2023 · 5 comments
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself default arguments Feature: default arguments for value parameters expressions Feature: expressions function parameters Feature → declarations: function parameters function types Feature → types: function types function values Feature → expressions: function values not a bug Resolution → not a bug: Reported as a bug but turned out to be expected behavior or programmer error swift 5.8 type checker Area → compiler: Semantic analysis

Comments

@andrewkykoo
Copy link

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
@andrewkykoo andrewkykoo added bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. triage needed This issue needs more specific labels labels Apr 12, 2023
@xedin
Copy link
Member

xedin commented Apr 12, 2023

This is expected behavior, in swift values of function type do not have default values applied, showNum has type (Int?) -> Void

@andrewkykoo
Copy link
Author

This is expected behavior, in swift values of function type do not have default values applied, showNum has type (Int?) -> Void

Since this is an expected behavior, do you suggest not assigning functions that take default parameters to variables of function type?

@xedin
Copy link
Member

xedin commented Apr 12, 2023

They can be assigned, just don’t expect that they can be called without providing arguments.

@andrewkykoo
Copy link
Author

Okay. I will keep in mind that variables of function type that are assigned functions with default parameters have to be called with arguments. In other words, they don't take default arguments.

At one point, I was wondering if the type could look like this, but this is simply not a correct type defining syntax for variables of function type.

var showNum: (Int? = 10) -> Void

Thanks for clarifying this!

@AnthonyLatsis
Copy link
Collaborator

AnthonyLatsis commented Apr 12, 2023

There is no reason not to pass around functions with defaulted parameters as values, but as Pavel mentioned, function types do not carry info about default values or arguments labels, so as soon as you turn a function declaration reference into a function value, all that info is lost. All that is left is the function type, with no association to a function declaration. For example, showNum(num: 10) is also invalid, you can only do showNum(10).

Moreover, the type-checker does not attempt to guess evaluation results (e.g. that showNum in showNum() references showNumber). Roughly speaking, all it cares about is the kind of expression (showNum is a reference to a variable) and its type (showNum is of type (Int) -> Void).

@AnthonyLatsis AnthonyLatsis added type checker Area → compiler: Semantic analysis default arguments Feature: default arguments for value parameters swift 5.8 function types Feature → types: function types function parameters Feature → declarations: function parameters compiler The Swift compiler in itself function values Feature → expressions: function values expressions Feature: expressions not a bug Resolution → not a bug: Reported as a bug but turned out to be expected behavior or programmer error and removed triage needed This issue needs more specific labels labels Apr 12, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself default arguments Feature: default arguments for value parameters expressions Feature: expressions function parameters Feature → declarations: function parameters function types Feature → types: function types function values Feature → expressions: function values not a bug Resolution → not a bug: Reported as a bug but turned out to be expected behavior or programmer error swift 5.8 type checker Area → compiler: Semantic analysis
Projects
None yet
Development

No branches or pull requests

3 participants