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

Context parameters #366

Merged
merged 29 commits into from
Sep 11, 2024
Merged

Context parameters #366

merged 29 commits into from
Sep 11, 2024

Conversation

serras
Copy link
Contributor

@serras serras commented Dec 21, 2023

Only comment here about the text itself. The discussion about the feature is done in this issue.

This is an updated proposal for KEEP-259, formerly known as context receivers. The new design addresses the issues raised by the users of the prototype and across the community.

proposals/context-parameters.md Outdated Show resolved Hide resolved
logWithTime("hello") // ambiguity error

context(console: ConsoleLogger, file: FileLogger) fun example1b() =
logWithTime("hello") // ambiguity error
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn't logWithTime be unresolved here instead of an ambiguity error? From my understanding, the type of a named context parameter isn't part of the scope inside the function. Or am I wrong here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, in every case logWithTime is resolved to the top-level function. The difference is whether there's ambiguity or not in the "context resolution" phase.

proposals/context-parameters.md Outdated Show resolved Hide resolved
proposals/context-parameters.md Outdated Show resolved Hide resolved
proposals/context-parameters.md Outdated Show resolved Hide resolved

**§26** *(applicability, `DslMarker`)*: During context resolution, if at a certain scope there is a potential contextual value in scope (either coming from a context parameter or from an implicit receiver) marked with an annotation `@X` which is itself annotated with [`@DslMarker`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-dsl-marker/) then:

- It is an _error_ for two such values to be available in the same scope.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Even if only one of them is applicable?


- It is an _error_ for two such values to be available in the same scope.
- If context resolution chooses a contextual value with the same annotation, but in an outer scope, it is a compilation _error_.
- If a call binds to a receiver with the same annotation, it is a compilation _error_.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TBH, I don't understand what this part means. May you please give some example?

proposals/context-parameters.md Outdated Show resolved Hide resolved

**§2.1** *(`context` function)*: The `context` function adds a new value to the context, in an anonymous manner.

* The implementation may be built into the compiler, instead of having a plethora of functions defined in the standard library.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This likely would complicate providing documentation for them.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We might need to be a bit more creative here. Maybe the regular Dokka-style docs are not good in this case; I would imagine this being part of the language documentation, given how central they are going to be to the context parameters feature itself.

```

We strongly advise _against_ creating "copies" of an API but with an added context parameter.
Overload resolution is tricky, and the developer may end up calling the unintended version.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The example doesn't illustrate what is dangerous in this case.

@serras serras merged commit 6d2e403 into master Sep 11, 2024
@serras serras mentioned this pull request Sep 11, 2024
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 this pull request may close these issues.

8 participants