Skip to content

[SR-504] Functions with default values cannot be called as parameters #43121

Open
@swift-ci

Description

@swift-ci
Previous ID SR-504
Radar rdar://problem/51939453
Original Reporter Yoontsaw (JIRA User)
Type Bug

Attachment: Download

Environment

OS X 10.11, Xcode(swift 2.1)/REPL(swift 2.2-dev)

Additional Detail from JIRA
Votes 1
Component/s Compiler
Labels Bug, TypeChecker
Assignee None
Priority Medium

md5: b01e85e40f3b2a14293af5843699a96c

is duplicated by:

  • SR-4878 Allow mapping of signatures with defaulted arguments
  • SR-749 References to functions do not include default parameters

Issue Description:

When I create a function like the codes below, when I call this function, the IDE actually gives two candidates, one of which is of type Int -> Int, and the other of type (Int, Int) -> Int.
But when I want to feed this function as a parameter of another function, the "Int -> Int" one is not working and produces the error as below.

  1> func tested(x: Int, y: Int = 0) -> Int { return x + y } 
  2. func testor(f: (Int) -> Int, x: Int) -> Int { return f(x) } 
  3. testor(tested, x: 0)
rep.swift:3:8: error: cannot convert value of type '(Int, y: Int) -> Int' to expected argument type '(Int) -> Int'
testor(tested, x: 0)
       ^~~~~~

This bug causes big problem when the parameter function is pre-defined with default values. When you see multiple overloads of that function e.g. Int tested(x: Int) and Int tested(x: Int, y: Int), you don't know if they were DEFINED by overloading or by default values. But when put it as parameter, the former works while the latter doesn't.
This behaviour is also demonstrated by the snapshots attached.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugA deviation from expected or documented behavior. Also: expected but undesirable behavior.compilerThe Swift compiler itselftype checkerArea → compiler: Semantic analysis

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions