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

Calling a function for each element of an array of mixed elements conforming to a same protocol #67198

Closed
LyricApps opened this issue Jul 10, 2023 · 1 comment
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler itself constrained existentials Feature → existentials: constrained existentials such as 'any Collection<Int>' duplicate Resolution: Duplicates another issue existential member accesses Feature → existentials: existential member accesses existentials Feature: values of types like `any Collection`, `Any` and `AnyObject`; type-erased values implicit existential opening Feature → existentials: implicit opening of existentials when passed to parameters of generic type parameterized protocols Feature → protocol: protocols with primary associated types swift 5.9 type checker Area → compiler: Semantic analysis

Comments

@LyricApps
Copy link

LyricApps commented Jul 10, 2023

Description
I am trying to call a function for each element of an array of mixed elements conforming to a same protocol.

Please consider the following code, the export and exportA compile fine but as soon as I add a requirement that Database inherits from a class then exportB won't compile.

protocol DatabaseExporter<Database> {
	associatedtype Database
	
	func export(database: Database)
}

func export<Database>(database: Database, usingExporters exporters: [any DatabaseExporter<Database>]) {
	for exporter in exporters {
		exporter.export(database: database)
	}
}

protocol A {}

func exportA<Database: A>(database: Database, usingExporters exporters: [any DatabaseExporter<Database>]) {
	for exporter in exporters {
		exporter.export(database: database)
	}
}

class B {}

func exportB<Database: B>(database: Database, usingExporters exporters: [any DatabaseExporter<Database>]) {
	for exporter in exporters {
		exporter.export(database: database) /// Error : Member 'export' cannot be used on value of type 'any DatabaseExporter<Database>'; consider using a generic constraint instead
		exportB(database: database, usingExporter: exporter) /// Error : Global function 'exportB(database:usingExporter:)' requires that 'Database' inherit from 'B'
	}
}

func exportB<Database: B>(database: Database, usingExporter exporter: some DatabaseExporter<Database>) {
	exporter.export(database: database)
}

Steps to reproduce
Try to compile the code above.

Expected behavior
It should compile.

Environment

  • swift-driver version: 1.75.2 Apple Swift version 5.8.1 (swiftlang-5.8.0.124.5 clang-1403.0.22.11.100)
    Target: arm64-apple-macosx13.0
  • Xcode 14.3.1
    Build version 14E300c
@LyricApps LyricApps added bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. triage needed This issue needs more specific labels labels Jul 10, 2023
@AnthonyLatsis AnthonyLatsis added duplicate Resolution: Duplicates another issue compiler The Swift compiler itself type checker Area → compiler: Semantic analysis existentials Feature: values of types like `any Collection`, `Any` and `AnyObject`; type-erased values swift 5.9 implicit existential opening Feature → existentials: implicit opening of existentials when passed to parameters of generic type existential member accesses Feature → existentials: existential member accesses parameterized protocols Feature → protocol: protocols with primary associated types constrained existentials Feature → existentials: constrained existentials such as 'any Collection<Int>' and removed triage needed This issue needs more specific labels labels Jul 10, 2023
@AnthonyLatsis
Copy link
Collaborator

Duplicate of #66794

@AnthonyLatsis AnthonyLatsis marked this as a duplicate of #66794 Jul 10, 2023
@AnthonyLatsis AnthonyLatsis closed this as not planned Won't fix, can't repro, duplicate, stale Jul 10, 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 itself constrained existentials Feature → existentials: constrained existentials such as 'any Collection<Int>' duplicate Resolution: Duplicates another issue existential member accesses Feature → existentials: existential member accesses existentials Feature: values of types like `any Collection`, `Any` and `AnyObject`; type-erased values implicit existential opening Feature → existentials: implicit opening of existentials when passed to parameters of generic type parameterized protocols Feature → protocol: protocols with primary associated types swift 5.9 type checker Area → compiler: Semantic analysis
Projects
None yet
Development

No branches or pull requests

2 participants