Skip to content

Clock interface should be a "fun" interface #273

@dalewking

Description

@dalewking

Since it is a SAM should be declared as a fun interface to allow using a lambda to provide an implementation.

Activity

ilya-g

ilya-g commented on May 11, 2023

@ilya-g
Member

When an interface has a single abstract method, it doesn't automatically qualify to be a functional interface.

We can however consider providing a SAM-like constructor function for Clock, e.g.

fun Clock(instantProvider: () -> Instant): Clock
dalewking

dalewking commented on May 12, 2023

@dalewking
Author

That would be fine too. I am looking to use dependency injection to inject Clock instance into code and to be able to inject an instance that I can easily control.

What I am doing now is that I have:

class OverridableClock : Clock {
    var instant: Instant? = null
    override fun now() = instant ?: Clock.System.now()

    operator fun plusAssign(duration: Duration) {
        instant = now() + duration
    }
}

I guess though since you you have a companion object I can go ahead and create such a function as an extension.

linked a pull request that will close this issue on Jun 6, 2023
kevincianfarini

kevincianfarini commented on Dec 15, 2023

@kevincianfarini

For what it's worth, requiring that Clock be a SAM would break proposals like #17 for very little benefit.

dalewking

dalewking commented on May 8, 2024

@dalewking
Author

For what it's worth, requiring that Clock be a SAM would break proposals like #17 for very little benefit.

Given that #17 seems to now be going the way of not changing Clock interface and #382 proposes moving Clock to the standard library, i once again propose that Clock be made a a fun interface.

Agreed it is a ver small benefit, but it also has basically zero cost

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      Participants

      @dalewking@ilya-g@kevincianfarini

      Issue actions

        Clock interface should be a "fun" interface · Issue #273 · Kotlin/kotlinx-datetime