Smallville7123
04/09/2019, 7:00 AMSlackbot
04/10/2019, 7:46 AMSmallville7123
04/10/2019, 9:47 AMclass A {
inner class B {
inner class C {
var empty: Int = 0
}
var empty: Int = 0
var a: MutableList<C>
init {
a = mutableListOf(C())
}
}
var a: MutableList<B>
init {
a = mutableListOf(B())
}
/**
* test variable
*/
var empty: Int = 0
}
fun getDeclaringUpperLevelClassObject(objectA: Any?): Any? {
if (objectA == null) {
return null
}
val cls = objectA.javaClass ?: return objectA
val outerCls = cls.enclosingClass
?: // this is top-level class
return objectA
// get outer class object
var outerObj: Any? = null
try {
val fields = cls.declaredFields
for (field in fields) {
if (field != null && field.type === outerCls
&& field.name != null && field.name.startsWith("this$")
) {
/*
`field.isAccessible = true` does not work with a Security Manager
java.security.AccessControlException: access denied
("java.lang.reflect.ReflectPermission" "suppressAccessChecks")
*/
field.isAccessible = true
outerObj = field.get(objectA)
break
}
}
} catch (e: Exception) {
e.printStackTrace()
}
return outerObj
}
/**
* returns a [MutableList] of classes parenting the current class
*
* the top most class is always the last index
*
* the last class is always the first index
*
* for example: up(f[0].a[0].a[0]) = 0(f[0].a[0].a[0]) 1(f[0].a[0]) 2(f[0])
*/
fun up(a: Any, m: MutableList<Any> = mutableListOf(), debug: Boolean = false): MutableList<Any> {
m.add(a)
if (debug) println("get upper class of ${a.javaClass.name}")
val upperC = getDeclaringUpperLevelClassObject(a)
if (upperC == null) abort("upperC is null o.o")
if (a.equals(upperC)) return m
else {
return up(upperC, m)
}
}
/**
* use as follows: `instanceChain(`[up]`(f[0]))`
*/
fun instanceChain(chain: MutableList<Any>, index: Int = chain.lastIndex, debug: Boolean = false): Any {
return if (index == 0) {
chain[index]
} else {
if (debug) println("chain[$index] = " + chain[index])
val outer = chain[index]
val toRun = Class.forName(chain[index].javaClass.name + "$" + chain[index - 1].javaClass.simpleName)
val ctor = toRun.getDeclaredConstructor(chain[index]::class.java)
val lowerCInstance = ctor.newInstance(outer)
if (debug) println("lowerCInstance = " + lowerCInstance!!::class.java)
if (index == 1) lowerCInstance
else instanceChain(chain, index - 1)
}
}
fun Test() {
val f = mutableListOf<A>()
f.add(A()) // this is required, i do not know how to do accomplish this in the init block
println("f[0] = ${instanceChain(up(f[0]))}")
println("f[0].a[0] = ${instanceChain(up(f[0].a[0]))}")
println("f[0].a[0].a[0] = ${instanceChain(up(f[0].a[0].a[0]))}")
abort()
}
Paul N
04/11/2019, 6:47 PMHullaballoonatic
04/11/2019, 8:28 PMMutableList
both an interface and a class in the stdlib without their names clashing? Do they have separate jvmNames?Joe
04/12/2019, 6:13 PMMap<String, Thing?>
is there an equivalent of Iterable#filterNoNull()
that will give me a Map<String,Thing>
for all non-null values?oday
04/13/2019, 12:53 PMvar mileageValuesToYears = mapOf(
1980 to 900000,
1981 to 900000,
1982 to 100000,
1983 to 120000
)
Akram
04/13/2019, 1:37 PMSlackbot
04/15/2019, 4:15 AMSrikumar
04/15/2019, 3:29 PModay
04/15/2019, 3:29 PMcolljos
04/16/2019, 12:58 PModay
04/17/2019, 8:07 AMmatt tighe
04/17/2019, 7:05 PMfun reportError(errorState: ErrorState) {
val type = object : Exception {
type.className = errorState.className
}
silentlyReportException(type)
handleDifferentErrorStates(errorState)
}
cspotcode
04/17/2019, 9:10 PMopen class SuperClass(val foo: String, val bar: String) {}
open class SubClass : SuperClass {}
val instance = SubClass(foo = "hello", bar = "world") // <-- subclass has same constructor signature as superclass without needing to re-type the signature in SubClass's declaration
Slackbot
04/18/2019, 6:05 AMSmallville7123
04/18/2019, 6:25 AMSmallville7123
04/18/2019, 6:26 AMfun String?.tokenizeVararg(vararg delimiters: String, returnDelimiters: Boolean = false): List<String>? {
return if (this == null) null
else this.tokenizeVararg(delimiters = delimiters, returnDelimiters = returnDelimiters)
}
Assigning single elements to varargs in named form is forbidden
Type mismatch: inferred type is Array<out String> but String was expected
Smallville7123
04/18/2019, 12:18 PMNumber
for example fun Number.comparisionsGreaterThan(i: Number): Boolean {
return this < i
}
Slackbot
04/19/2019, 1:32 AMAry Sugiarto
04/19/2019, 9:19 AModay
04/19/2019, 4:36 PMsophiataskova
04/20/2019, 3:04 AMOleh Ponomarenko
04/22/2019, 2:04 PMNovoa
04/23/2019, 11:15 PMfun foo(): String = "bar"
What I understand is that function foo takes no parameters and has a return type of String. What is odd to me coming from other languages where I have never seen this, is that it is valid for there to be no body declaration but just an equal sign after the return type assigning the return value as a constant. If you could, would you please provide a use case for a function declared similarly?Nezteb
04/24/2019, 4:55 PMmapOf
have any form that tapes type parameters?
Like mapOf<String, Int>(...)
?df
04/25/2019, 9:39 AMwhile (true) {
val rows = repo.findAll(page)
if (rows.isEmpty()) {
break
}
doSomething(rows)
}
Tim Fennis
04/25/2019, 1:09 PMbjonnh
04/26/2019, 8:16 PMbjonnh
04/26/2019, 8:52 PM@SolrDocument(solrCoreName ="indexedcitation")
class IndexedCitation {
@Id
@Indexed(name = "id", type = "string")
private var id: String? = null
@Indexed(name = "title", type = "string")
private var title: String? = null
fun getId(): String? {
return id
}
fun setId(id: String?) {
this.id = id
}
fun getTitle(): String? {
return title
}
fun setTitle(title: String?) {
this.title = title
}
}
but not:
@SolrDocument(solrCoreName ="indexedcitation")
data class IndexedCitation(
@Id
@Indexed(name = "id", type = "string")
private var id: String? = null,
@Indexed(name = "title", type = "string")
private var title: String? = null
)