Hi, I'm trying to play around with context receive...
# server
r
Hi, I'm trying to play around with context receivers to restrict where methods can be called from? Example: I have a UserRepository that can create users, but I only want to be able to call that from the UserService
Copy code
data class User(val name: String)

interface UserRepository {
	context(UserService.Context)
	fun createUser(): User
}

class RealUserRepository : UserRepository {
	context(UserService.Context)
	override fun createUser() = User("random-string")
}

interface UserService {
    class Context private constructor()
    companion object {
        private val context: Context = Context() // How do I create here?
    }

    //extension function so it's "protected"
	fun <T> UserService.withContext(block: context(Context) () -> T): T = with(context) {
        block(this)
    }

    fun createUser(): User
}

class RealUserService : UserService {
	val userRepository: UserRepository = RealUserRepository()

	override fun createUser() = withContext {
		userRepository.createUser()
	}
}
Everything works except the initialization of the Context. 1. Is there another way to accomplish this without context receivers? 2. How do I properly initialize the companion object prop?