|
| 1 | +//> using options -source future |
| 2 | +import language.experimental.namedTuples |
| 3 | +import scala.compiletime.asMatchable |
| 4 | + |
| 5 | +type City = (name: String, zip: Int, pop: Int) |
| 6 | +type Raw = (String, Int, Int) |
| 7 | + |
| 8 | +type Coord = (x: Double, y: Double) |
| 9 | +type Labels = (x: String, y: String) |
| 10 | + |
| 11 | +@main def Test = |
| 12 | + val city: City = (name = "Lausanne", zip = 1000, pop = 140000) |
| 13 | + val coord: Coord = (x = 1.0, y = 0.0) |
| 14 | + val labels: Labels = (x = "west", y = "north") |
| 15 | + |
| 16 | + val size: 3 = city.size |
| 17 | + assert(city.size == 3) |
| 18 | + |
| 19 | + val zip: Int = city(1) |
| 20 | + assert(zip == 1000) |
| 21 | + |
| 22 | + val name: String = city.head |
| 23 | + assert(name == "Lausanne") |
| 24 | + |
| 25 | + val zip_pop: (zip: Int, pop: Int) = city.tail |
| 26 | + val (_: Int, _: Int) = zip_pop |
| 27 | + assert(zip_pop == (zip = 1000, pop = 140000)) |
| 28 | + |
| 29 | + val cinit = city.init |
| 30 | + val _: (name: String, zip: Int) = cinit |
| 31 | + assert(cinit == (name = "Lausanne", zip = 1000)) |
| 32 | + |
| 33 | + val ctake1: (name: String) = city.take(1) |
| 34 | + assert(ctake1 == (name = "Lausanne")) |
| 35 | + |
| 36 | + val cdrop1 = city.drop(1) |
| 37 | + val _: (zip: Int, pop: Int) = cdrop1 |
| 38 | + assert(cdrop1 == zip_pop) |
| 39 | + |
| 40 | + val cdrop3 = city.drop(3) |
| 41 | + val _: NamedTuple.Empty = cdrop3 |
| 42 | + assert(cdrop3 == NamedTuple.Empty) |
| 43 | + |
| 44 | + val cdrop4 = city.drop(4) |
| 45 | + val _: NamedTuple.Empty = cdrop4 |
| 46 | + assert(cdrop4 == NamedTuple.Empty) |
| 47 | + |
| 48 | + val csplit = city.splitAt(1) |
| 49 | + val _: ((name: String), (zip: Int, pop: Int)) = csplit |
| 50 | + assert(csplit == ((name = "Lausanne"), zip_pop)) |
| 51 | + |
| 52 | + val city_coord = city ++ coord |
| 53 | + val _: NamedTuple.Concat[City, Coord] = city_coord |
| 54 | + val _: (name: String, zip: Int, pop: Int, x: Double, y: Double) = city_coord |
| 55 | + assert(city_coord == (name = "Lausanne", zip = 1000, pop = 140000, x = 1.0, y = 0.0)) |
| 56 | + |
| 57 | + type IntToString[X] = X match |
| 58 | + case Int => String |
| 59 | + case _ => X |
| 60 | + |
| 61 | + val intToString = [X] => (x: X) => x.asMatchable match |
| 62 | + case x: Int => x.toString |
| 63 | + case x => x |
| 64 | + |
| 65 | + val citymap = city.map[IntToString](intToString.asInstanceOf) |
| 66 | + val _: (name: String, zip: String, pop: String) = citymap |
| 67 | + assert(citymap == (name = "Lausanne", zip = "1000", pop = "140000")) |
| 68 | + |
| 69 | + val cityreverse = city.reverse |
| 70 | + val _: (pop: Int, zip: Int, name: String) = cityreverse |
| 71 | + assert(cityreverse == (pop = 140000, zip = 1000, name = "Lausanne")) |
| 72 | + |
| 73 | + val zipped = coord.zip(labels) |
| 74 | + val _: (x: (Double, String), y: (Double, String)) = zipped |
| 75 | + val (x3, y3) = zipped |
| 76 | + val _: (Double, String) = x3 |
| 77 | + assert(zipped == (x = (1.0, "west"), y = (0.0, "north"))) |
| 78 | + |
| 79 | + val zippedRaw = ((1.0, "west"), (0.0, "north")) |
| 80 | + val (x1: (Double, String), x2: (Double, String)) = zippedRaw |
| 81 | + |
| 82 | + val cityFields = city.toList |
| 83 | + val _: List[String | Int] = cityFields |
| 84 | + assert(cityFields == List("Lausanne", 1000, 140000)) |
| 85 | + |
| 86 | + val citArr = city.toArray |
| 87 | + val _: List[String | Int] = cityFields |
| 88 | + assert(cityFields == List("Lausanne", 1000, 140000)) |
| 89 | + |
0 commit comments