Skip to content

Commit

Permalink
Generate interface typealiases before the protocol.
Browse files Browse the repository at this point in the history
  • Loading branch information
tristanlabelle committed Mar 23, 2024
1 parent 02ee970 commit 66e1d48
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@ import WindowsMetadata
// Interfaces are generated as two types: a protocol and an existential typealias.
// Given an interface IFoo, we generate:
//
// protocol IFooProtocol { ... }
// typealias IFoo = any IFooProtocol
// protocol IFooProtocol { ... }
//
// This provides a more natural (C#-like) syntax when using those types:
//
// var foo: IFoo? = getFoo()
internal func writeInterfaceDefinition(_ interface: InterfaceDefinition, projection: SwiftProjection, to writer: SwiftSourceFileWriter) throws {
try writeProtocol(interface, projection: projection, to: writer)
try writeProtocolTypeAlias(interface, projection: projection, to: writer)
try writeProtocol(interface, projection: projection, to: writer)

let typeName = try projection.toProtocolName(interface)
switch interface.name {
Expand Down
16 changes: 8 additions & 8 deletions Generator/Sources/SwiftWinRT/Writing/NamespaceAlias.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,6 @@ internal func writeNamespaceAliasesFile(typeDefinitions: [TypeDefinition], modul
}

internal func writeNamespaceAlias(_ typeDefinition: TypeDefinition, projection: SwiftProjection, to writer: SwiftSourceFileWriter) throws {
if let interface = typeDefinition as? InterfaceDefinition {
try writer.writeProtocol(
visibility: SwiftProjection.toVisibility(interface.visibility),
name: projection.toProtocolName(interface, namespaced: false),
typeParams: interface.genericParams.map { $0.name },
bases: [projection.toBaseProtocol(interface)]) { _ in }
}

try writer.writeTypeAlias(
visibility: SwiftProjection.toVisibility(typeDefinition.visibility),
name: projection.toTypeName(typeDefinition, namespaced: false),
Expand All @@ -31,6 +23,14 @@ internal func writeNamespaceAlias(_ typeDefinition: TypeDefinition, projection:
name: projection.toTypeName(typeDefinition),
genericArgs: typeDefinition.genericParams.map { SwiftType.identifier(name: $0.name) }))

if let interface = typeDefinition as? InterfaceDefinition {
try writer.writeProtocol(
visibility: SwiftProjection.toVisibility(interface.visibility),
name: projection.toProtocolName(interface, namespaced: false),
typeParams: interface.genericParams.map { $0.name },
bases: [projection.toBaseProtocol(interface)]) { _ in }
}

if typeDefinition is InterfaceDefinition || typeDefinition is DelegateDefinition {
try writer.writeTypeAlias(
visibility: SwiftProjection.toVisibility(typeDefinition.visibility),
Expand Down

0 comments on commit 66e1d48

Please sign in to comment.