Недавно обнаружил для себя несколько интересных применений object в Kotlin. Как ни странно, обо всех сказано в официальной документации, но на практике я не часто встречал их применение.
1️⃣Создание анонимного класса с нуля. Вдруг у вас будут кейсы (если подскажите, какие, то буду рад), когда вам необходимо создать объект с нуля без объявления класса. Вот пример: val helloWorld = object { val hello = "Hello" val world = "World" // object expressions extend Any, so override is required on toString() override fun toString() = "$hello $world" }
А ещё можно сделать вот такие мудрёные конструкции: open class A(x: Int) { public open val y: Int = x }
interface B { /*...*/ }
val ab: A = object : A(1), B { override val y = 15 }
2️⃣Object как Singleton — довольно распространённый кейс, когда вы делаете аналог синглтона Java в Kotlin.
3️⃣Data objects — экспериментальная, но любопытная фича, позволяющая делать так: data object MyObject
fun main() { println(MyObject) // MyObject }
А посмотреть все применения Object можно как раз в статье. 🤓
Недавно обнаружил для себя несколько интересных применений object в Kotlin. Как ни странно, обо всех сказано в официальной документации, но на практике я не часто встречал их применение.
1️⃣Создание анонимного класса с нуля. Вдруг у вас будут кейсы (если подскажите, какие, то буду рад), когда вам необходимо создать объект с нуля без объявления класса. Вот пример: val helloWorld = object { val hello = "Hello" val world = "World" // object expressions extend Any, so override is required on toString() override fun toString() = "$hello $world" }
А ещё можно сделать вот такие мудрёные конструкции: open class A(x: Int) { public open val y: Int = x }
interface B { /*...*/ }
val ab: A = object : A(1), B { override val y = 15 }
2️⃣Object как Singleton — довольно распространённый кейс, когда вы делаете аналог синглтона Java в Kotlin.
3️⃣Data objects — экспериментальная, но любопытная фича, позволяющая делать так: data object MyObject
fun main() { println(MyObject) // MyObject }
А посмотреть все применения Object можно как раз в статье. 🤓
In the “Bear Market Screaming Therapy Group” on Telegram, members are only allowed to post voice notes of themselves screaming. Anything else will result in an instant ban from the group, which currently has about 75 members. According to media reports, the privacy watchdog was considering “blacklisting” some online platforms that have repeatedly posted doxxing information, with sources saying most messages were shared on Telegram. Commenting about the court's concerns about the spread of false information related to the elections, Minister Fachin noted Brazil is "facing circumstances that could put Brazil's democracy at risk." During the meeting, the information technology secretary at the TSE, Julio Valente, put forward a list of requests the court believes will disinformation. The imprisonment came as Telegram said it was "surprised" by claims that privacy commissioner Ada Chung Lai-ling is seeking to block the messaging app due to doxxing content targeting police and politicians. Telegram offers a powerful toolset that allows businesses to create and manage channels, groups, and bots to broadcast messages, engage in conversations, and offer reliable customer support via bots.
from us