Skip to content

Unify Builders #171

@05nelsonm

Description

@05nelsonm

Builders for implementations need to be unified. A Function object is being created every time and Java consumers always need to return Unit. Additionally, static implementations are not being re-used when builder configurations are the same; new instances are always being created.

Migrate all implementations to a EncoderName.Builder class, with associated lambda functions for Kotlin consumers on the encoder implementation's companion object.

e.g.

public class EncoderName {

    public class Builder {
        public constructor(): this(other = null)
        public constructor(other: Config?) {
            // ...
        }

        // ...

        public fun build(): EncoderName = Config.build(this)
    }

    public companion object {
        @JvmStatic
        @JvmName("-Builder")
        @OptIn(ExperimentalContracts::class)
        public inline fun Builder(block: Builder.() -> Unit): EncoderName {
            contract { callsInPlace(block, InvocationKind.EXACTLY_ONCE) }
            Builder(other = null, block)
        }

        @JvmStatic
        @JvmName("-Builder")
        @OptIn(ExperimentalContracts::class)
        public inline fun Builder(other: Config?, block: Builder.() -> Unit): EncoderName {
            contract { callsInPlace(block, InvocationKind.EXACTLY_ONCE) }
            return Builder(other).apply(block).build()
        }
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions