https://kotlinlang.org logo
Join SlackCommunities
Powered by
# effective-kotlin
  • a

    amanda.hinchman-dominguez

    02/22/2019, 10:13 PM
    It's very weak
  • a

    amanda.hinchman-dominguez

    02/22/2019, 10:13 PM
    Looking at recursing because I forgot about parsing internal functions šŸ˜ž
  • a

    amanda.hinchman-dominguez

    02/22/2019, 10:14 PM
    and there's probably way more I missed
  • a

    amanda.hinchman-dominguez

    02/22/2019, 10:16 PM
    To be fair,
    when
    statements did totally save my life but without being able to use sealed classes I've been forced to "poke and prod"
  • a

    alostpacket

    02/22/2019, 10:39 PM
    can you point to a line where you found the casting frustrating?
  • a

    amanda.hinchman-dominguez

    02/23/2019, 9:05 PM
    Okay, here's one example: this is one I desperately wish to refactor because the code is so ugly
  • a

    amanda.hinchman-dominguez

    02/23/2019, 9:06 PM
    property_getting.kt
    property_getting.kt
  • a

    amanda.hinchman-dominguez

    02/23/2019, 9:07 PM
    Oh, I may have insight since I first wrote this over 6 months ago (before I even saw your talk on sealed classes) on some potential refactoring tips and refactored a little but but not much. However, I feel like if I didn't have this string check, I'd have to wrap whens - is there a way to check if a Node has a child type but it is not an immediate child, or do we just keep on pattern matching down
  • a

    alostpacket

    02/24/2019, 3:31 AM
    Not sure If I am 100% clear on what you mean by "has a child type" (is the child a field on the Node? Or do you mean some other class somewhere is a child of Node?)
  • a

    alostpacket

    02/24/2019, 3:31 AM
    either way there may be a trick with
    when
    and Ranges that can help you
  • a

    alostpacket

    02/24/2019, 3:32 AM
    on the left side of the branch you can evaluate if something is
    in
    a Range, but you can overload the
    contains()
    operator
    to make it check just about anything...
  • a

    alostpacket

    02/24/2019, 3:34 AM
    Copy code
    operator fun Regex.contains(text: CharSequence): Boolean = this.containsMatchIn(text)
    
    class Substring(private val str: String) {
      operator fun contains(text: CharSequence): Boolean = text.contains(str)
    }
  • a

    alostpacket

    02/24/2019, 3:37 AM
    then you can write something like:
    Copy code
    when ("hello") {
        in Regex("h*llo") -> println("Regex")
        in Substring("ello") -> println("Substring")
    }
    😮 1
  • a

    alostpacket

    02/24/2019, 5:10 AM
    This is really just syntactic sugar though as you can put anything on the left that returns a Boolean
  • l

    louiscad

    02/24/2019, 12:29 PM
    The
    Substring
    class could be inline BTW
    šŸ‘ 2
  • t

    Tristan

    05/21/2019, 6:36 AM
    as per my post in #general I’m keen join/start conversations about how to leverage what Kotlin provides to write code that’s clear at the site of use - because we spend most of our time reading code, not writing it. this helps others (and a lot of the time, ā€œothersā€ means ā€œfuture me who’s forgotten why he wrote this in the pastā€ šŸ˜‰) https://kotlinlang.slack.com/archives/C0922A726/p1558405181100800
    • 1
    • 1
  • f

    Fudge

    05/21/2019, 6:45 AM
    Something I've noticed about Kotlin is it does a good job of enforcing its ideals on you (in a good way)
  • f

    Fudge

    05/21/2019, 6:46 AM
    Like final by default classes, and smart casts from immutabillty
  • f

    Fudge

    05/21/2019, 6:47 AM
    I guess one tip for effective kotlin would be to avoid mutable values and
    lateinit
  • f

    Fudge

    05/21/2019, 6:48 AM
    I've heard people saying that you should never have nullable variables in kotlin, and "It's just a remnant from Java". What do you think about that?
    t
    d
    r
    • 4
    • 4
  • f

    Fudge

    06/21/2019, 7:40 PM
    I've been seeing all these libraries using type-safe builders in a way that seems insane It basically goes like this (as a simplified example): Instead of having
    Copy code
    class User(val name: String,val email:String)
    They do
    Copy code
    class User{
    var name : String 
    var email: String
    }
    fun user(init: User.() -> Unit) = User.apply{init()}
    What are the benefits of this?
    t
    • 2
    • 3
  • f

    Fudge

    06/24/2019, 5:45 PM
    I would love to get anyone to comment on this discussion (on slack) https://github.com/mipt-npm/plotly.kt/issues/12
  • k

    Khan

    08/10/2022, 9:43 AM
    Hi. I need to open a BottomSheetFragment on button click. What is the better approach 1- Should i create a global instance and create it once
    Copy code
    val dialog by lazy { Dialog() }
    2- Should i create it inside method each time button is clicked
    Copy code
    btn.setOnClickListener { showDialog() }
    
    fun showDialog() {
        Dialog().show()
    }
    f
    • 2
    • 1
  • k

    Kev

    08/30/2022, 2:54 PM
    Hello, is there a way to tell the compiler to place my data classes as inner classes to an object/interface type so that I can define my data classes in different files, but still have them as inner classes to use as namespacing?
  • g

    Gordon

    11/18/2022, 6:18 AM
    Does lateinit has a performance cost? I mean it adds additional if check for every access to the variable?
    f
    • 2
    • 1
  • k

    Kev

    11/15/2023, 10:54 AM
    Greetings, is it possible to create an extension function on a specific Enum value?
    🚫 1
    x
    • 2
    • 1
  • s

    Shreyas

    11/19/2024, 4:07 PM
    Here why the "is there" string not get concatenated?
  • d

    Dinesh Sharma

    12/02/2024, 12:23 PM
    because your + statement is part of else ...
  • d

    Dinesh Sharma

    12/02/2024, 12:23 PM
    or rather will only execute in else case..
  • d

    Dinesh Sharma

    12/02/2024, 12:24 PM
    test it by changing the value of a = "C" and see if it prints C is there