torture.kt 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. /** KDoc for the file's package. */
  2. package com.example.torture
  3. import com.example.other.OtherClass
  4. import com.example.util.helper
  5. import com.example.wild.*
  6. import com.example.alias.LongName as Short
  7. // line comment run 1
  8. // line comment run 2
  9. fun topLevel(x: Int, s: String): WidgetK {
  10. val local = x + 1
  11. return WidgetK(local)
  12. }
  13. /** KDoc on extension fn. */
  14. fun WidgetK.extend(n: Int): Int {
  15. render()
  16. return n
  17. }
  18. fun <T> List<T>.genericExt(): T = first()
  19. fun com.example.Qualified.qext() {}
  20. suspend fun suspender(): Unit { helper() }
  21. private internal fun visFn() {}
  22. fun inferred() = helper()
  23. fun nullableRet(): WidgetK? = null
  24. fun lambdaRet(): (Int) -> Unit = { }
  25. expect fun platformThing(): Int
  26. actual fun actualThing(): Int = 1
  27. tailrec fun tailer(n: Int): Int = if (n <= 0) 0 else tailer(n - 1)
  28. infix fun Int.pow(e: Int): Int = this
  29. operator fun WidgetK.plus(o: WidgetK): WidgetK = this
  30. val topVal: Int = 3
  31. var topVar = "s"
  32. const val TOP_CONST = 99
  33. val topDelegated by lazy { WidgetK(1) }
  34. val (destA, destB) = makePair()
  35. val withGetter: Int
  36. get() = 42
  37. class WidgetK(val size: Int, private var name: String = defaultName()) {
  38. val area: Int = size * size
  39. var label: String? = null
  40. val computed: Int
  41. get() = size * 2
  42. init {
  43. val initLocal = 5
  44. register(initLocal)
  45. }
  46. constructor(s: String) : this(s.length) {
  47. log(s)
  48. }
  49. fun render(): Unit {
  50. draw(size)
  51. }
  52. fun chainInner(): WidgetK = this
  53. companion object {
  54. val SHARED = WidgetK(0)
  55. const val COMPANION_CONST = 7
  56. fun create(): WidgetK = WidgetK(1)
  57. }
  58. companion object Named { }
  59. }
  60. data class DataK(val a: Int, val b: String)
  61. abstract class AbstractK {
  62. abstract fun impl(): Int
  63. }
  64. open class OpenBase(n: Int) {
  65. open fun over() {}
  66. }
  67. class SubK(n: Int) : OpenBase(n), Drawable, Comparable<SubK> {
  68. override fun over() {}
  69. override fun compareTo(other: SubK): Int = 0
  70. override fun draw() {}
  71. }
  72. class QualifiedSuper : com.example.deep.RemoteBase() { }
  73. class DelegatedImpl(d: Drawable) : Drawable by d
  74. interface Drawable {
  75. fun draw()
  76. fun outline(): Int = 1
  77. val prop: Int get() = 2
  78. }
  79. sealed class SealedOp {
  80. object Add : SealedOp()
  81. data class Mul(val f: Int) : SealedOp()
  82. }
  83. sealed interface SealedIface
  84. enum class Color {
  85. RED, GREEN, BLUE
  86. }
  87. enum class Http(val code: Int) {
  88. OK(200) {
  89. override fun label(): String = "ok"
  90. },
  91. ERR(500) {
  92. override fun label(): String = "err"
  93. };
  94. abstract fun label(): String
  95. fun common(): Int = code
  96. companion object {
  97. fun of(c: Int): Http = OK
  98. }
  99. }
  100. object Registry {
  101. val instances = mutableListOf<WidgetK>()
  102. var count = 0
  103. const val REG_CONST = 1
  104. fun register(w: WidgetK) { instances.add(w) }
  105. }
  106. annotation class MyMarker(val why: String = "")
  107. @MyMarker
  108. class Annotated {
  109. @JvmStatic
  110. fun jvmStatic() {}
  111. @Deprecated("gone", ReplaceWith("new"))
  112. fun old() {}
  113. @field:JvmField
  114. val fielded: Int = 1
  115. @get:MyMarker
  116. val got: Int = 2
  117. }
  118. typealias Handler = (Int) -> Unit
  119. typealias WidgetList = List<WidgetK>
  120. expect class PlatformFile {
  121. fun path(): String
  122. }
  123. actual class ActualFile {
  124. actual fun path(): String = "/"
  125. }
  126. actual typealias PlatformClock = java.time.Clock
  127. fun caller() {
  128. val w = WidgetK(1)
  129. w.render()
  130. this.toString()
  131. super.hashCode()
  132. Registry.register(w)
  133. Registry.count
  134. Color.RED
  135. com.example.Fq.CONST_READ
  136. WidgetK.create().render()
  137. Foo.getInstance().bar()
  138. lowerFactory().chain()
  139. w.chainInner().render()
  140. "literal".uppercase()
  141. 5.toString()
  142. listOf(1, 2).size
  143. w.label?.length
  144. w.label!!.length
  145. helper()
  146. Short.static()
  147. val fn: Handler = { i -> println(i) }
  148. fn(3)
  149. (fn)(4)
  150. run { helper() }
  151. listOf(1).forEach { it + 1 }
  152. w.let { it.render() }
  153. generic<Int>(1)
  154. register(::topLevel)
  155. register(OtherClass::handle)
  156. register(w::render)
  157. register(this::caller)
  158. obtain(String::class)
  159. val m = ::caller
  160. val bound = w::render
  161. val s = "interp $topVal and ${w.render()} end"
  162. val multi = """raw $topVal"""
  163. when (w.size) {
  164. 1 -> helper()
  165. else -> draw(0)
  166. }
  167. if (topVal > 1) { helper() }
  168. for (i in 1..3) { draw(i) }
  169. fun localFn(): Int = 5
  170. localFn()
  171. class LocalClass {
  172. fun lm() {}
  173. }
  174. object LocalObj {
  175. fun om() {}
  176. }
  177. val anon = object : Drawable {
  178. override fun draw() { helper() }
  179. }
  180. anon.draw()
  181. label@ for (i in 1..2) { break@label }
  182. val backtick = `weird name`()
  183. }
  184. fun `weird name`(): Int = 1
  185. fun trailing(block: (Int) -> Int): Int = block(1)
  186. fun useTrailing() {
  187. trailing { it * 2 }
  188. trailing() { it * 3 }
  189. }
  190. fun defaults(a: Int = compute(), b: String = "x") {}
  191. fun varargFn(vararg xs: Int) {}
  192. fun destructuringBody(p: Pair<Int, Int>) {
  193. val (x, y) = p
  194. draw(x + y)
  195. }
  196. fun assignRefs() {
  197. Registry.count = 5
  198. Registry.count += 1
  199. }
  200. fun nullish(x: WidgetK?) {
  201. x?.render()
  202. val l = x ?: WidgetK(0)
  203. }
  204. fun stringsEdge() {
  205. val a = "quote \" and dollar ${'$'} done"
  206. }
  207. fun labeledLambda() {
  208. listOf(1).forEach loop@{ if (it == 0) return@loop }
  209. }
  210. fun whereClause(): Int where Int : Comparable<Int> = 1