Skip to content

Using generics in method definitions produces un-compilable output #1407

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

Open
Jonas1893 opened this issue Mar 27, 2025 · 2 comments
Open

Using generics in method definitions produces un-compilable output #1407

Jonas1893 opened this issue Mar 27, 2025 · 2 comments

Comments

@Jonas1893
Copy link

sourcery version: 2.2.6
swift version: 6.0.3

Using the following protocol

// sourcery: AutoMockable
public protocol Foo {

    @discardableResult
    func register<C, V>(key: Key, coder: C) -> TypedKey<V> where C: Coder, C.Value == V
}

produces the following output:

public class MockFoo: Foo {

    public init() {}



    //MARK: - register<C, V>

    public var registerCVKeyKeyCoderCTypedKeyVCallsCount = 0
    public var registerCVKeyKeyCoderCTypedKeyVCalled: Bool {
        return registerCVKeyKeyCoderCTypedKeyVCallsCount > 0
    }
    public var registerCVKeyKeyCoderCTypedKeyVReceivedArguments: (key: Key, coder: C)? // compiler error: Cannot find type 'C' in scope
    public var registerCVKeyKeyCoderCTypedKeyVReceivedInvocations: [(key: Key, coder: C)] = [] // compiler error: Cannot find type 'C' in scope
    public var registerCVKeyKeyCoderCTypedKeyVReturnValue: TypedKey<V> where C: Coder, C.Value == V! // multiple compiler issues
    public var registerCVKeyKeyCoderCTypedKeyVClosure: ((Key, C) -> TypedKey<V> where C: Coder, C.Value == V)? // multiple compiler issues
    @discardableResult
    public func register<C, V>(key: Key, coder: C) -> TypedKey<V> where C: Coder, C.Value == V {
        registerCVKeyKeyCoderCTypedKeyVCallsCount += 1
        registerCVKeyKeyCoderCTypedKeyVReceivedArguments = (key: key, coder: coder)
        registerCVKeyKeyCoderCTypedKeyVReceivedInvocations.append((key: key, coder: coder))
        if let registerCVKeyKeyCoderCTypedKeyVClosure = registerCVKeyKeyCoderCTypedKeyVClosure {
            return registerCVKeyKeyCoderCTypedKeyVClosure(key, coder)
        } else {
            return registerCVKeyKeyCoderCTypedKeyVReturnValue
        }
    }

}

Here I have no idea how to solve this as we can't use generics for the registerCVKeyKeyCoderCTypedKeyVReturnValue definition
Maybe it is best to omit the generation of registerCVKeyKeyCoderCTypedKeyVReturnValue and registerCVKeyKeyCoderCTypedKeyVClosure if it can't be solved automatically

@iharandreyev
Copy link

Same issue here. Default template ignores where clauses

@sonic555gr
Copy link

Any suggestions on the how to fix this issue?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants