swift-server / swift-aws-lambda-runtime

Swift implementation of AWS Lambda Runtime
Apache License 2.0
1.12k stars 101 forks source link

Use primary associated type to reduce boilerplate #266

Open stevapple opened 2 years ago

stevapple commented 2 years ago

After SE-0346 lands in Swift 5.7, it’s possible for us to mark Event and Output as primary associate types for LambdaHandler. This can simplify a demo handler from:

@main
struct MyLambda: LambdaHandler {
    typealias Event = String
    typealias Output = String

    init(context: LambdaInitializationContext) async throws {
        // setup your resources that you want to reuse for every invocation here.
    }

    func handle(_ input: String, context: LambdaContext) async throws -> String {
        // as an example, respond with the input's reversed
        String(input.reversed())
    }
}

into

@main
struct MyLambda: LambdaHandler<String, String> {
    init(context: LambdaInitializationContext) async throws {
        // setup your resources that you want to reuse for every invocation here.
    }

    func handle(_ input: String, context: LambdaContext) async throws -> String {
        // as an example, respond with the input's reversed
        String(input.reversed())
    }
}
stevapple commented 2 years ago

Note that the demo doesn’t work at the moment because parameterized protocol support is not fully implemented yet. It is valid and hopefully we could use it starting from 5.7.