You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/topics/type-safe-builders.md
+34-2Lines changed: 34 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -242,7 +242,40 @@ html {
242
242
}
243
243
```
244
244
245
-
## Full definition of the com.example.html package
245
+
You can also apply the `@DslMarker` annotation directly to [function types](lambdas.md#function-types).
246
+
Simply annotate the `@DslMarker` annotation with `@Target(AnnotationTarget.TYPE)`:
247
+
248
+
```kotlin
249
+
@Target(AnnotationTarget.TYPE)
250
+
@DslMarker
251
+
annotationclassHtmlTagMarker
252
+
```
253
+
254
+
As a result, the `@DslMarker` annotation can be applied to function types, most commonly to lambdas with receivers. For example:
255
+
256
+
```kotlin
257
+
funhtml(init: @HtmlTagMarker HTML.() ->Unit): HTML { ... }
258
+
259
+
fun HTML.head(init: @HtmlTagMarker Head.() ->Unit): Head { ... }
260
+
261
+
fun Head.title(init: @HtmlTagMarker Title.() ->Unit): Title { ... }
262
+
```
263
+
264
+
When you call these functions, the `@DslMarker` annotation restricts access to outer receivers in the body of a lambda marked with it unless you specify them explicitly:
265
+
266
+
```kotlin
267
+
html {
268
+
head {
269
+
title {
270
+
// Access to title, head or other functions of outer receivers is restricted here.
271
+
}
272
+
}
273
+
}
274
+
```
275
+
276
+
Only the nearest receiver's members and extensions are accessible within a lambda, preventing unintended interactions between nested scopes.
277
+
278
+
### Full definition of the com.example.html package
246
279
247
280
This is how the package `com.example.html` is defined (only the elements used in the example above).
248
281
It builds an HTML tree. It makes heavy use of [extension functions](extensions.md) and
@@ -345,4 +378,3 @@ fun html(init: HTML.() -> Unit): HTML {
0 commit comments