• Hullaballoonatic

    Hullaballoonatic

    3 years ago
    nullable constructor params can be assigned at initialization to non-nullable lateinit properties. If they are null, then the property remains uninitialized, otherwise it is set.
    class Foo(bar: Int?, baz: Int?, boz: Int?) {
        lateinit var bar: Int = bar // acceptable
        lateinit var baz = baz // implicitly assigned as non-nullable
        lateinit var boz: Int? = baz // unacceptable
    }
    Hullaballoonatic
    Pavlo Liapota
    9 replies
    Copy to Clipboard
  • b

    bbaldino

    3 years ago
    am i understanding it right that using
    withDefault
    for a map doesn't actually insert that default into the map when a key which isn't present is found, it just returns an instance of that default?
    b
    nfrankel
    +2
    12 replies
    Copy to Clipboard
  • m

    Marc Knaup

    3 years ago
    Any plans for a standard multiplatform
    Locale
    yet? It would be needed for a multiplatform time library to support formatting 🙂
    m
    simon.vergauwen
    +1
    10 replies
    Copy to Clipboard
  • arekolek

    arekolek

    3 years ago
    Could some contract be added to
    Int.sign
    to support this?
    val foo = when (a.compareTo(b).sign) {
        -1 -> SomeEnum.LEFT
        0 -> SomeEnum.NONE
        1 -> SomeEnum.RIGHT
    }
    Does the contract API support something like this?
    arekolek
    karelpeeters
    +1
    8 replies
    Copy to Clipboard
  • b

    bbaldino

    3 years ago
    i notice that, when subclassing
    MutableMap
    , overriding
    compute
    gives the signature
    override fun compute(key: T, remappingFunction: BiFunction<in T, in U?, out U?>): U?
    , but overriding
    computeIfAbsent
    gives
    override fun computeIfAbsent(key: T, mappingFunction: Function<in T, out U>): U
    (note the missing
    ?
    after
    U
    on the return type) but
    computeIfAbsent
    is documented as being able to return null. is this a bug?
    computeIfPresent
    also correctly uses
    U?
    as the return type.
    b
    uli
    9 replies
    Copy to Clipboard
  • Hullaballoonatic

    Hullaballoonatic

    3 years ago
    operator fun <T> Pair<T, T>.iterator() = object : Iterator<T> {
        var i = 0
        
        override fun hasNext() = i < 2
        
        override fun next() = when (i++) {
            0 -> first
            1 -> second
            else -> throw NoSuchElementException()
        }
    }
    Hullaballoonatic
    p
    +3
    18 replies
    Copy to Clipboard
  • Hullaballoonatic

    Hullaballoonatic

    3 years ago
    "coordinate" iteration, e.g
    (0 to 0)..(1 to 1)
    iterates through
    0 to 0, 0 to 1, 1 to 0, 1 to 1
    . Might be a stretch, but I think it's intuitive.
    operator fun Pair<Int, Int>.rangeTo(that: Pair<Int, Int>) = object : Iterable<Pair<Int, Int>> {
        override fun iterator() = object : Iterator<Pair<Int, Int>> {
            var i = this@rangeTo.first
            var j = this@rangeTo.second
            
            val m = that.first - i
            val n = that.second - j
    
            override fun hasNext() = i <= m && j <= n
    
            override fun next(): Pair<Int, Int> {
                val res = i to j
                
                if (j == n) {
                    j = 0
                    i++
                } else {
                    j++
                }
                
                return res
            }
        }
    }
    could also be expanded to higher dimensions with
    List<Int>.rangeTo(other: List<Int>)
    ,
    IntArray
    , etc
    Hullaballoonatic
    Dico
    +1
    10 replies
    Copy to Clipboard
  • Hullaballoonatic

    Hullaballoonatic

    3 years ago
    Expand on
    String::split
    to be as great as joinToString:
    fun String.split(
        delimiter: CharSequence = ", ",
        prefix: CharSequence = "",
        postfix: CharSequence = "",
        limit: Int = -1
    ): List<String>
    fun <R> String.split(
        delimiter: CharSequence = ", ",
        prefix: CharSequence = "",
        postfix: CharSequence = "",
        limit: Int = -1,
        transform: (CharSequence) -> R
    ): List<R>
    Hullaballoonatic
    1 replies
    Copy to Clipboard
  • s

    serebit

    3 years ago
    Superclass information in KType (via typeOf) would be nice
    s
    Dico
    5 replies
    Copy to Clipboard
  • j

    jw

    3 years ago
    so ClockMarks aren't Comparable, which makes sense since you cannot really meaningfully compare them across Clocks, but also it's a bit annoying since frequently you're in a single Clock "namespace" where it would be nice to not have to continually unwrap them
    j
    ilya.gorbunov
    11 replies
    Copy to Clipboard