youssef hachicha
11/07/2024, 7:59 AMmbonnin
11/07/2024, 5:06 PM// How do I do this in Kotlin?
interface Shape {
int area();
class Square implements Shape {
// private constructor here
private Square() {}
@Override public int area() {return 0;}
}
// more shapes...
/**
* static factory method
* Doesn't work on a Kotlin companion function because the Square constructor is private
*/
static Square square() {
return new Square();
}
}
Bilagi
11/10/2024, 8:24 AMAndrew O'Hara
11/11/2024, 9:07 PMclass BusinessLogic(
val getMessage: () -> String
)
I want to build a factory method that would let me override getMessage
with some static String.
fun createBusinessLogic(messageOverride: String? = null): BusinessLogic
I initially came up with this:
fun createBusinessLogic(
messageOverride: String? = null
) = BusinessLogic(
getMessage = if (messageOverride != null) {
{ messageOverride }
} else {
val random = Random(1337)
{ "Message ${random.nextInt()}" }
}
)
Which fails to compile, because the compiler thinks the () -> String
I'm trying to return is an argument to the Random
constructor. My workaround was to assign the lambda to a val
, and then return that on the next line.
fun createBusinessLogic(
messageOverride: String? = null
) = BusinessLogic(
getMessage = if (messageOverride != null) {
{ messageOverride }
} else {
val random = Random(1337)
val supplier = {
"Message ${random.nextInt()}"
}
supplier
}
)
But it's weird! And ugly! I feel like there should be a better way to do this, without resorting to something drastic like:
fun createBusinessLogic(
messageOverride: String? = null
) = if (messageOverride != null) {
BusinessLogic { messageOverride }
} else {
val random = Random(1337)
BusinessLogic { "Message ${random.nextInt()}"}
}
Which won't really fly in my real app, because the real-life BusinessLogic
has several more arguments that would be duplicated.Abhilash Mandaliya
11/13/2024, 10:47 AMserialVersionUID
for my classes implementing Serializable
? I found several plugin options or doing it manually but none of the plugins work with the latest version of IDEA? Isn't there any default support like Java?Stylianos Gakis
11/20/2024, 2:51 PMcopy()
function, only with the parameters that I need for each use case which will just create a new instance of the data class, just like the normal copy
function would.
Any reasons why I should not be doing that?Gilles Barbier
11/21/2024, 8:18 AMjuliocbcotta
12/04/2024, 12:28 PMNorbi
12/25/2024, 6:15 PMgroostav
01/19/2025, 7:33 AMScanner
, but it demands delimeters that I dont want to give it.
I was also hoping kotli might have a kind of reader + regex extension function, something like <http://java.io|java.io>.Reader.takeWhile(regex: Regex)
, but no luck there either.
What does a quintessential kotlin text tokenizer look like?Abhilash Mandaliya
01/24/2025, 6:40 AMsairaham@kotlin-dev-ThinkPad-P53:~/workspace/kotlin-dev-connect$ ./gradlew build -x test
Starting a Gradle Daemon (subsequent builds will be faster)
FAILURE: Build failed with an exception.
* Where:
Build file '/home/sairaham/workspace/kotlin-dev-connect/build.gradle.kts' line: 57
* What went wrong:
Class org.jetbrains.kotlin.cli.common.CompilerSystemProperties does not have member field 'org.jetbrains.kotlin.cli.common.CompilerSystemProperties COMPILE_INCREMENTAL_WITH_ARTIFACT_TRANSFORM'
* Try:
> Run with --stacktrace option to get the stack trace.
> Run with --info or --debug option to get more log output.
> Run with --scan to get full insights.
> Get more help at <https://help.gradle.org>.
BUILD FAILED in 6s
5 actionable tasks: 1 executed, 4 up-to-date
I have no clue what is wrong here. Google search also didn't help with it. Any help is greatly appreciated 🙏Andrew O'Hara
02/03/2025, 5:07 PMEugen Mayer
02/07/2025, 9:00 AMSlackbot
02/10/2025, 10:40 AMCaleb B
02/24/2025, 3:58 PMMap<Class<T>, Supplier<T>>
, where T
is NOT a type parameter of the enclosing class but instead "pinning" the type of the supplier to the type of the class for runtime type validation? Instead of Map<Class<*>, Supplier<*>>
and having to suppress constant type coercion warningsRaj Paliwal
02/28/2025, 5:40 PMOTHER_REASON
• Description: ScreenOffCheckKill 26m3s4ms (26.33344%) threshold 2.0%
The issue occurs when a*fter screen turns off, app in background.*
The description in the exit logs suggests ScreenOffCheckKill, but I couldn’t find any official documentation explaining this behavior.
• What exactly does ScreenOffCheckKill
mean, and why does it happen?
• Is this a system-level restriction or a configurable setting?
• How can I prevent my app from getting killed due to this reason?
• Any pointers to official documentation or similar cases would be highly appreciated.
Thanks in advance for any insights!Amir H. Ebrahimnezhad
03/20/2025, 3:12 PMAmir H. Ebrahimnezhad
04/04/2025, 6:17 PMSebastian Schuberth
04/05/2025, 8:16 PMAutoClosables
) but for Kotlin code and use
?christophsturm
05/25/2025, 6:38 PMCaleb B
05/28/2025, 9:06 PMMyBuilder.() -> Unit
parameter, you can obviously have the extensions be members of the builder class to be implicitly available, but I can't figure out how to do that with someone else's without making the end user either import them manually or use a with
block like below.
Does anyone know how I could implicitly let people use this bundle of extensions in the DSL without the with
block?
[Comment = original builder, code = my WIP syntax]Caleb B
06/11/2025, 8:27 PMfun foo(arg1: String, arg2: String) {
// ...
}
// Known:
fun bar(arg1: String, arg2: String) = foo(arg1, arg2)
// Ideal?
fun bar = foo
Caleb B
06/11/2025, 8:58 PMval default: Int
set(value: Int) { field = value }
set(value: IntSupplier) { field = value.get() }
// so this can happen:
mybuilder {
// either this:
default = 10
// or this:
default = { longOperationToFetchDefault() }
}
aishwaryabhishek3
06/25/2025, 6:35 AM// Sealed class without generics
sealed class Result
// Subclass with a generic type
data class Success<T>(val data: T) : Result()
// Subclass without a generic type
object Loading : Result()
Or
sealed class Result<T>
class Success<T>(val data: T) : Result<T>()
// Some other subclass that doesn't need T can be defined as
class Error(val message: String) : Result<Nothing>()
Manuel Lorenzo
07/09/2025, 1:40 PMCaleb B
07/09/2025, 11:55 PM"foo" in ("bar", "baz", "qux")
instead of "foo" in listOf("bar, "baz", "qux")
so it doesn't read as cluttered.
This one might actually be impossible since it would require fun (vararg T).contains
, but it would be nice to do myList -= "x", "y", "z"
Caleb B
07/10/2025, 12:01 AM=
) with kotlin-assignment-plugin, but is there a way to do the same for is
?
I love making DSLs. It's so much fun to screw with the language to make code more readable.Kev
08/03/2025, 7:57 AMdecision.first
is a DecisionResult.Next
without a manual class? Its not getting inferred for some reason.Amir H. Ebrahimnezhad
08/10/2025, 4:49 PMGeorgeS-Litesoft
08/20/2025, 5:59 PM