Skip to content
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

Clock interface should be a "fun" interface #273

Open
dalewking opened this issue May 11, 2023 · 4 comments · May be fixed by #279
Open

Clock interface should be a "fun" interface #273

dalewking opened this issue May 11, 2023 · 4 comments · May be fixed by #279

Comments

@dalewking
Copy link

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

@ilya-g
Copy link
Member

ilya-g commented May 11, 2023

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
Copy link
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.

@severn-everett severn-everett linked a pull request Jun 6, 2023 that will close this issue
@kevincianfarini
Copy link

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

@dalewking
Copy link
Author

dalewking commented May 8, 2024

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
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants
@dalewking @ilya-g @kevincianfarini and others