swiftlang / swift

The Swift Programming Language
https://swift.org
Apache License 2.0
67.32k stars 10.34k forks source link

Using packs in closure: cannot fully abstract a value of variadic function type. #69412

Open CrystDragon opened 11 months ago

CrystDragon commented 11 months ago

Description I want to implement a variadic version of the currying function.

func curried<First, each T, Result>(
  _ originalFunc: @escaping (First, repeat each T) -> Result
) -> (First) -> (repeat each T) -> Result {
  return { (first: First) in
    return { (remanings: repeat each T) -> Result in
      return originalFunc(first, repeat each remanings)
    }
  }
}

The error message is:

error: cannot fully abstract a value of variadic function type '(repeat each T) -> Result' because different contexts will not be able to reliably agree on a calling convention; try wrapping it in a struct
error: cannot fully abstract a value of variadic function type '(repeat each T) -> Result' because different contexts will not be able to reliably agree on a calling convention; try wrapping it in a struct

I have no idea what it means by " try wrapping it in a struct", it seems unrelated.

Environment

AnthonyLatsis commented 10 months ago

I can’t reproduce the error with just the code you provided. Was there e.g. a call site that you forgot to include?

CrystDragon commented 10 months ago

I can’t reproduce the error with just the code you provided. Was there e.g. a call site that you forgot to include?

Ah, I didn't notice this, the code I provided only fails in playground. I put it in a command line tool project, and it compiles fine.

CrystDragon commented 10 months ago

Demo.playground.zip @AnthonyLatsis I uploaded a demo for you.

rjmccall commented 10 months ago

If the playground transform is trying to capture a variadically-generic function as some sort of opaque value like an Any, that is unfortunately not going to work.