Klitos Kyriacou
09/09/2022, 8:09 AMreturn new InterfaceXXX() {
@Override public void foo() {}
@Override public void bar() {}
}
But Kotlin uses the object
keyword:
return object : InterfaceXXX {
override fun foo() {}
override fun bar() {}
}
And because it uses the object
keyword, it looks as if you are returning the same singleton instance every time. I have to do a double-take to notice it's not actually a singleton. Does this confuse others too?frogger
09/09/2022, 8:58 AMz = null
here instead of an exception? (try)
fun main() {
val (x, y, z) = "A/B".split("/")
}
Ben Edwards
09/09/2022, 12:12 PMfor (i in binding) {
if (i is Button) i.btnPlayVideo.setOnClickListener(this)
}
I get why as you cant do a for iteration on bindings but what would be alternative code for this?
package com.funkytwig.youtubeplayer
import android.os.Bundle
import android.os.PersistableBundle
import android.util.Log
import android.view.View
import android.widget.Button
import androidx.appcompat.app.AppCompatActivity
import com.funkytwig.youtubeplayer.databinding.ActivityStandaloneBinding
import com.google.android.youtube.player.YouTubeStandalonePlayer
class StandaloneActivity : AppCompatActivity(), View.OnClickListener { // android.view.View
private val TAG = "StandaloneActivity"
private lateinit var binding: ActivityStandaloneBinding
override fun onCreate(savedInstanceState: Bundle?) {
Log.d(TAG, "onCreate")
super.onCreate(savedInstanceState)
binding = ActivityStandaloneBinding.inflate(layoutInflater)
setContentView(binding.root)
for (i in binding) {
if (i is Button) i.btnPlayVideo.setOnClickListener(this)
}
// Long form version of code
// binding.btnPlayVideo.setOnClickListener(this)
// binding.btnPlayPlaylist.setOnClickListener(this)
}
override fun onClick(view: View) { // Ctrl+I to implement interface
TODO("Needs implementing")
}
}
BenGasan
09/09/2022, 3:28 PMGavin Ray
09/10/2022, 3:46 PMclass DiskManagerImpl(file: File) : DiskManager {
private val channel = AsynchronousFileChannel.open(file.toPath(), StandardOpenOption.READ, StandardOpenOption.WRITE)
override suspend fun readPage(pageId: PageId, buffer: MemorySegment) {
val offset = pageId * PAGE_SIZE
val read = channel.readAsync(buffer.asByteBuffer(), offset.toLong())
require(read == PAGE_SIZE) { "Failed to read page $pageId" }
}
override suspend fun writePage(pageId: PageId, buffer: MemorySegment) {
val offset = pageId * PAGE_SIZE
val written = channel.writeAsync(buffer.asByteBuffer(), offset.toLong())
require(written == PAGE_SIZE) { "Failed to write page $pageId" }
}
}
ephemient
09/11/2022, 5:47 AMval a: Double = Double.NaN
println(a == a) // false
val b: Number = a
println(a == b) // true
val c: Double = 0.0
println(c == -c) // true
val d: Number = c
println(d == -c) // false
(yes, I know why this has to happen for IEEE 754 and boxed equals() reasons, it's a fun puzzler to figure out if you don't know why though)Ben Edwards
09/11/2022, 2:26 PM@file:Suppress("DEPRECATION")
package com.funkytwig.flickerbrowser
import android.os.AsyncTask
import android.util.Log
import java.io.IOException
import java.net.MalformedURLException
import java.net.URL
enum class DowsloadStatus {
OK, IDLE, NOT_INSTALLED, FAILED_OR_EMPTY, PERMISSIONS_ERROR, ERROR
}
private const val TAG = "GetRawData"
@Suppress("DEPRECATION")
class GetRawData : AsyncTask<String, Void, String>() { // class
private var downloadStatus = DowsloadStatus.IDLE
@Deprecated("Deprecated in Java")
override fun doInBackground(vararg params: String?): String {
// params[0] = JSON URL
val funct = "doInBackground"
Log.d(TAG, funct)
Log.d(TAG, "$funct ${params[0]}")
if (params[0] == null) {
Log.d(TAG, "$funct No URL")
downloadStatus = DowsloadStatus.NOT_INSTALLED
return "No URL Specified"
}
try {
downloadStatus = DowsloadStatus.OK
Log.d(TAG, "$funct: DowsloadStatus.OK")
return URL(params[0]).readText()
} catch (e: Exception) {
val errorMessage = when (e) {
is MalformedURLException -> {
downloadStatus = DowsloadStatus.NOT_INSTALLED
"Invalid URL ${e.message}"
}
is IOException -> {
downloadStatus = DowsloadStatus.FAILED_OR_EMPTY
"IO Exception ${e.message}"
}
is SecurityException -> {
downloadStatus = DowsloadStatus.PERMISSIONS_ERROR
"Needs Permission ${e.message}"
}
else -> {
downloadStatus = DowsloadStatus.ERROR
"Unknown Error ${e.message}"
}
}
Log.d(TAG, "$funct dome errorMessage")
return errorMessage
} // try/catch
} // doInBackground
@Deprecated("Deprecated in Java")
override fun onPostExecute(result: String?) {
val funct = "onPostExecute"
Log.d(TAG, funct)
super.onPostExecute(result)
}
@Deprecated("Deprecated in Java")
override fun onCancelled() {
val funct = "onCancelled"
Log.d(TAG, funct)
super.onCancelled()
}
@Deprecated("Deprecated in Java")
override fun onPreExecute() {
val funct = "onPreExecute"
Log.d(TAG, funct)
super.onPreExecute()
}
@Deprecated("Deprecated in Java")
override fun onProgressUpdate(vararg values: Void?) {
val funct = "onProgressUpdate"
Log.d(TAG, funct)
super.onProgressUpdate(*values)
}
}
Any ideas?Philip Graf
09/12/2022, 9:18 AMpublic class Pet {
private List<String> names;
public @NonNull List<@NonNull String> getNames() {
return names;
}
// constructor, setter, etc. omitted
}
I tried to write the following data class:
data class Pet(val names: @get:NonNull List<@get:NonNull String>)
For each of the annotations I get two compile errors:
- This annotation is not applicable to target 'undefined target' and use site target '@get'
- This annotation is not applicable to target 'type usage' and use site target '@get'
The NonNull annotation is from the Java library microprofile-graphql-api
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE_USE, ElementType.METHOD, ElementType.FIELD, ElementType.PARAMETER})
@Documented
public @interface NonNull {
}
I run into the same problem when I try to set the annotations on the field:
data class Pet(val names: @field:NonNull List<@field:NonNull String>)
Is there a way to get annotations on the type and generic type of a getter method or field?Joan Killeen
09/12/2022, 9:26 AMjeggy
09/12/2022, 3:05 PMinterface Criteria<T: Criteria<T>> { fun test(): T }
open class Filter1(val data: String = ""): Criteria<Filter1> {
override fun test() = Filter1("Basic")
}
object Standard: Filter1() {}
// How can I provide some correct type here?
fun whatever(): Criteria<*> {
// Some custom code...
return Standard
}
fun main() {
println(whatever().data)
}
Could someone help me with my whatever
function here, so it returns the correct type? (playground: https://pl.kotl.in/z4lPq_kWE)Alina Grebenkina
09/12/2022, 3:10 PMJan
09/12/2022, 6:23 PMGasan
09/13/2022, 9:19 AMKenneth
09/13/2022, 1:04 PMKristian Nedrevold
09/13/2022, 8:20 PModay
09/14/2022, 10:48 AMenum class TextRowState {
Enabled,
Disabled,
ActiveAlpha{
fun get(){
if (Enabled){
0.5f
}else{
1f
}
}
}
}
Laurent Laborde
09/14/2022, 5:08 PMzt
09/14/2022, 9:56 PMzt
09/15/2022, 12:29 AMBernhard
09/15/2022, 10:05 AMType argument is not within its bounds: should be subtype of 'PostgreSQLContainer<PostgreSQLContainer<*>!>!'
when instantiating it via PostgreSQLContainer<PostgreSQLContainer<*>>
this used to work in Kotlin 1.6. Java signature is
PostgreSQLContainer<SELF extends PostgreSQLContainer<SELF>> extends JdbcDatabaseContainer<SELF>
Bernhard
09/15/2022, 10:15 AMy
09/15/2022, 10:49 AMreturn@
. why is it needed? does it allow you to do something you previously couldn’t, or is it just syntax sugar?Laurent Laborde
09/15/2022, 4:52 PMMidoriya
09/16/2022, 3:11 AMBen Edwards
09/16/2022, 8:53 AMprivate const val TAG = "BaseActivity"
Which can put just after the imports. Its not defined within a class which seems inconsistent with private being '`private` means that the member is visible inside this class only (including all its members).'. Does it effectively become private to all classes in the file? To go further down the rabbit hole what does 'including all its members' mean? I guess it means function/methods but if so why not just say this?LastExceed
09/16/2022, 9:34 AMlistOf(1,2,3).sumOf { it.toDouble() } //works fine
listOf(1,2,3).sumOf { it.toFloat() } //error: no applicable overload
can someone explain why?jeggy
09/16/2022, 12:41 PMfun <T> List<Int>.transform(
block: (Int) -> T = { it }
): List<T> {
return map { block(it) }
}
fun main() {
val items = listOf(1, 2, 3)
println(items.transform { "$it, " })
println(items.transform())
}
Does anyone know if there is a issue on youtrack somewhere discussing making something like this possible?martmists
09/16/2022, 1:02 PMLaurent Laborde
09/16/2022, 8:22 PMLaurent Laborde
09/16/2022, 9:30 PM