torture.kt 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  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. val initLambda: () -> Unit = { caller() }
  38. val initSam = Runnable { caller() }
  39. val initObject = object : Runnable {
  40. override fun run() {
  41. caller()
  42. }
  43. }
  44. class WidgetK(val size: Int, private var name: String = defaultName()) {
  45. val area: Int = size * size
  46. var label: String? = null
  47. val computed: Int
  48. get() = size * 2
  49. init {
  50. val initLocal = 5
  51. register(initLocal)
  52. }
  53. constructor(s: String) : this(s.length) {
  54. log(s)
  55. }
  56. fun render(): Unit {
  57. draw(size)
  58. }
  59. fun chainInner(): WidgetK = this
  60. companion object {
  61. val SHARED = WidgetK(0)
  62. const val COMPANION_CONST = 7
  63. fun create(): WidgetK = WidgetK(1)
  64. }
  65. companion object Named { }
  66. }
  67. data class DataK(val a: Int, val b: String)
  68. abstract class AbstractK {
  69. abstract fun impl(): Int
  70. }
  71. open class OpenBase(n: Int) {
  72. open fun over() {}
  73. }
  74. class SubK(n: Int) : OpenBase(n), Drawable, Comparable<SubK> {
  75. override fun over() {}
  76. override fun compareTo(other: SubK): Int = 0
  77. override fun draw() {}
  78. }
  79. class QualifiedSuper : com.example.deep.RemoteBase() { }
  80. class DelegatedImpl(d: Drawable) : Drawable by d
  81. interface Drawable {
  82. fun draw()
  83. fun outline(): Int = 1
  84. val prop: Int get() = 2
  85. }
  86. sealed class SealedOp {
  87. object Add : SealedOp()
  88. data class Mul(val f: Int) : SealedOp()
  89. }
  90. sealed interface SealedIface
  91. enum class Color {
  92. RED, GREEN, BLUE
  93. }
  94. enum class Http(val code: Int) {
  95. OK(200) {
  96. override fun label(): String = "ok"
  97. },
  98. ERR(500) {
  99. override fun label(): String = "err"
  100. };
  101. abstract fun label(): String
  102. fun common(): Int = code
  103. companion object {
  104. fun of(c: Int): Http = OK
  105. }
  106. }
  107. object Registry {
  108. val instances = mutableListOf<WidgetK>()
  109. var count = 0
  110. const val REG_CONST = 1
  111. fun register(w: WidgetK) { instances.add(w) }
  112. }
  113. annotation class MyMarker(val why: String = "")
  114. @MyMarker
  115. class Annotated {
  116. @JvmStatic
  117. fun jvmStatic() {}
  118. @Deprecated("gone", ReplaceWith("new"))
  119. fun old() {}
  120. @field:JvmField
  121. val fielded: Int = 1
  122. @get:MyMarker
  123. val got: Int = 2
  124. }
  125. typealias Handler = (Int) -> Unit
  126. typealias WidgetList = List<WidgetK>
  127. expect class PlatformFile {
  128. fun path(): String
  129. }
  130. actual class ActualFile {
  131. actual fun path(): String = "/"
  132. }
  133. actual typealias PlatformClock = java.time.Clock
  134. fun caller() {
  135. val w = WidgetK(1)
  136. w.render()
  137. this.toString()
  138. super.hashCode()
  139. Registry.register(w)
  140. Registry.count
  141. Color.RED
  142. com.example.Fq.CONST_READ
  143. WidgetK.create().render()
  144. Foo.getInstance().bar()
  145. lowerFactory().chain()
  146. w.chainInner().render()
  147. "literal".uppercase()
  148. 5.toString()
  149. listOf(1, 2).size
  150. w.label?.length
  151. w.label!!.length
  152. helper()
  153. Short.static()
  154. val fn: Handler = { i -> println(i) }
  155. fn(3)
  156. (fn)(4)
  157. run { helper() }
  158. listOf(1).forEach { it + 1 }
  159. w.let { it.render() }
  160. generic<Int>(1)
  161. register(::topLevel)
  162. register(OtherClass::handle)
  163. register(w::render)
  164. register(this::caller)
  165. obtain(String::class)
  166. val m = ::caller
  167. val bound = w::render
  168. val s = "interp $topVal and ${w.render()} end"
  169. val multi = """raw $topVal"""
  170. when (w.size) {
  171. 1 -> helper()
  172. else -> draw(0)
  173. }
  174. if (topVal > 1) { helper() }
  175. for (i in 1..3) { draw(i) }
  176. fun localFn(): Int = 5
  177. localFn()
  178. class LocalClass {
  179. fun lm() {}
  180. }
  181. object LocalObj {
  182. fun om() {}
  183. }
  184. val anon = object : Drawable {
  185. override fun draw() { helper() }
  186. }
  187. anon.draw()
  188. label@ for (i in 1..2) { break@label }
  189. val backtick = `weird name`()
  190. }
  191. fun `weird name`(): Int = 1
  192. fun trailing(block: (Int) -> Int): Int = block(1)
  193. fun useTrailing() {
  194. trailing { it * 2 }
  195. trailing() { it * 3 }
  196. }
  197. fun defaults(a: Int = compute(), b: String = "x") {}
  198. fun varargFn(vararg xs: Int) {}
  199. fun destructuringBody(p: Pair<Int, Int>) {
  200. val (x, y) = p
  201. draw(x + y)
  202. }
  203. fun assignRefs() {
  204. Registry.count = 5
  205. Registry.count += 1
  206. }
  207. fun nullish(x: WidgetK?) {
  208. x?.render()
  209. val l = x ?: WidgetK(0)
  210. }
  211. fun stringsEdge() {
  212. val a = "quote \" and dollar ${'$'} done"
  213. }
  214. fun labeledLambda() {
  215. listOf(1).forEach loop@{ if (it == 0) return@loop }
  216. }
  217. fun whereClause(): Int where Int : Comparable<Int> = 1
  218. class AccessorK {
  219. val sameLineGetter: Int get() = compute()
  220. var sameLinePair: Int get() = compute()
  221. set(v) { draw(v) }
  222. }
  223. class SiblingAccessorK {
  224. var nextLine: Int = 0
  225. get() = compute()
  226. set(v) { draw(v) }
  227. val (localA, localB) = makePair()
  228. init {
  229. val fromInit = compute()
  230. register(fromInit)
  231. }
  232. }