Skip to content

Commit 405d98f

Browse files
committed
Inline structural type example
1 parent 951312f commit 405d98f

File tree

1 file changed

+19
-1
lines changed

1 file changed

+19
-1
lines changed

docs/docs/reference/changed/structural-types.md

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,25 @@ configure how fields and methods should be resolved.
2929

3030
## Example
3131

32-
<script src="https://scastie.scala-lang.org/Duhemm/HOZFKyKLTs294XOSYPU5Fw.js"></script>
32+
```scala
33+
object StructuralTypeExample {
34+
35+
case class Record(elems: (String, Any)*) extends Selectable {
36+
def selectDynamic(name: String): Any = elems.find(_._1 == name).get._2
37+
}
38+
39+
type Person = Record {
40+
val name: String
41+
val age: Int
42+
}
43+
44+
def main(args: Array[String]): Unit = {
45+
val person = Record("name" -> "Emma", "age" -> 42).asInstanceOf[Person]
46+
println(s"${person.name} is ${person.age} years old.")
47+
// Prints: Emma is 42 years old.
48+
}
49+
}
50+
```
3351

3452
## Extensibility
3553

0 commit comments

Comments
 (0)