Skip to content
This repository has been archived by the owner on Jun 5, 2023. It is now read-only.

context functions with implicit receiver #124

Open
robstoll opened this issue Jun 3, 2020 · 0 comments
Open

context functions with implicit receiver #124

robstoll opened this issue Jun 3, 2020 · 0 comments

Comments

@robstoll
Copy link

robstoll commented Jun 3, 2020

I was playing around a bit with context functions and IMO they aren't yet as useful as they could be when it comes to the builder pattern.
As a basis for discussion I take the table example from here https://dotty.epfl.ch/docs/reference/contextual/context-functions.html

Imagine I want to add a custom cell to a row, something like:

table {
  row { 
    cell("top left")
    add(new MyCell("qs"))  //add is unknown
  }
}

in order that I can do that I need to provide an add method which is using a Row similar to the existing cell method, basically re-publishing what is already on Row to the Context-Function world (reminds me a bit of async programming in C# where suddenly everything needs async).

def add(c: Cell)(using r: Row) = r.add(c)

IMO this boilerplate should not be necessary, it would be nice if the behaviour could be changed as follows:

  • if a context function accepts exactly 1 argument, then this argument is used as implicit receiver.
  • this inside such a function refers to the implicit receiver
  • one can use self or another identifier (don't know how the feature is called in scala) in the outer scope to refer to it from the context function (class A { self => ...)

But maybe it would make more sense to have a different syntax for such functions and de-sugar it to context functions behind the scene? In my opinion the syntax of Kotlin is quite nice: Row.() => Unit with the benefit that it allows to define arguments in addition which are not implicit in contrast to context functions where all arguments are implicit. For instance: Row.(Int) => Unit

I just saw that there is https://contributors.scala-lang.org/t/pre-sip-thisfunction-scope-injection-similar-to-kotlin-receiver-function but it is not active any more and without reading through the whole posts I deduce that there wasn't really a common sense. Let me know in case I should close this and continue the discussion in the aforementioned thread.

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

No branches or pull requests

1 participant