mp
10/25/2019, 6:41 PM@Nullable
for a method that returns a nullable type, etc, so that in plain Java code the IDE can provide the same suggestions that it would for a @Nullable
Java method?jossiwolf
11/03/2019, 7:49 PMExpressionCodegen
works? Like anything, I'm just trying to get started with a modification on it and trying to understand a bit morewiyarmir
11/04/2019, 4:24 PMtoString()
in a data class or is the limitation stated here still a problem? https://github.com/ZacSweers/redacted-compiler-pluginjereksel
11/04/2019, 10:45 PMjdemeulenaere
11/08/2019, 9:37 PMimport com.intellij.openapi.Disposable
import com.intellij.psi.PsiFileFactory
import org.jetbrains.kotlin.cli.common.CLIConfigurationKeys
import org.jetbrains.kotlin.cli.jvm.compiler.CliBindingTrace
import org.jetbrains.kotlin.cli.jvm.compiler.EnvironmentConfigFiles
import org.jetbrains.kotlin.cli.jvm.compiler.KotlinCoreEnvironment
import org.jetbrains.kotlin.cli.jvm.compiler.TopDownAnalyzerFacadeForJVM
import org.jetbrains.kotlin.config.CommonConfigurationKeys
import org.jetbrains.kotlin.config.CompilerConfiguration
import org.jetbrains.kotlin.config.JVMConfigurationKeys
import org.jetbrains.kotlin.config.JvmTarget
import org.jetbrains.kotlin.container.get
import org.jetbrains.kotlin.idea.KotlinLanguage
import org.jetbrains.kotlin.metadata.jvm.deserialization.JvmProtoBufUtil
import org.jetbrains.kotlin.psi.KtFile
import org.jetbrains.kotlin.resolve.BindingContext
import org.jetbrains.kotlin.resolve.LazyTopDownAnalyzer
import org.jetbrains.kotlin.resolve.TopDownAnalysisMode
import org.jetbrains.kotlin.resolve.lazy.declarations.FileBasedDeclarationProviderFactory
import kotlin.reflect.jvm.internal.impl.storage.StorageManager
class Analyzer {
private val compilerConfiguration = CompilerConfiguration().apply {
put(CommonConfigurationKeys.MODULE_NAME, JvmProtoBufUtil.DEFAULT_MODULE_NAME)
put(CLIConfigurationKeys.MESSAGE_COLLECTOR_KEY, StdOutMessageCollector)
put(JVMConfigurationKeys.JVM_TARGET, JvmTarget.JVM_1_8)
}
private val kotlinEnvironment = KotlinCoreEnvironment.createForProduction(
parentDisposable = Disposable { },
configuration = compilerConfiguration,
configFiles = EnvironmentConfigFiles.JVM_CONFIG_FILES
)
private val project = kotlinEnvironment.project
private val psiFactory = PsiFileFactory.getInstance(project)
fun analyzeCode(code: String): Pair<KtFile, BindingContext> {
val file = createKotlinFile(code)
return file to analyze(file)
}
private fun createKotlinFile(code: String): KtFile {
val file = psiFactory.createFileFromText("Dumb.kt", KotlinLanguage.INSTANCE, code, true, false)
return file as KtFile
}
private fun analyze(psiFile: KtFile): BindingContext {
val files = arrayListOf(psiFile)
val trace = CliBindingTrace()
val container = TopDownAnalyzerFacadeForJVM.createContainer(
project,
emptyList(),
trace,
kotlinEnvironment.configuration,
kotlinEnvironment::createPackagePartProvider,
{ storageManager, _ -> FileBasedDeclarationProviderFactory(storageManager, files) }
)
val analyzer = container.get<LazyTopDownAnalyzer>()
analyzer.analyzeDeclarations(TopDownAnalysisMode.TopLevelDeclarations, files)
return trace.bindingContext
}
}
jereksel
11/11/2019, 1:38 PMraulraja
11/12/2019, 11:03 PMFunctionDescriptor
that points to something like:
fun <A> A.show(): String = toString()
is a valid candidate to be applied in the expression
val s: String = 1.show()
When I have A
and Int
as `KotlinType`s and I ask if Int
is a subtype of A
it always says no despite being a generic type. This makes sense since it could refer to any other type. What service does the Kotlin compiler use to see if a generic function is in bounds or applicable to a type? Any help is appreciated, thanks.Fudge
11/18/2019, 7:56 PMkotlin-compiler
package to parse the Kotlin files, perform PSI renaming transformations, and then write the transformed PSI as text. However, when I try to use the PSI add
and replace
methods, I get the following error:
java.lang.IllegalArgumentException: Missing extension point: com.intellij.treeCopyHandler in area null
Is this the right way of going about this? If it is, how do I resolve this error?turansky
11/23/2019, 10:12 AMcompanion object
= I need assotiated IDEA plugin for comfort work with this companion object
?spierce7
11/25/2019, 4:34 AMjosephivie
11/26/2019, 4:34 AMkotlin_module
file before re-running the compiler on changed files, which leads to compilation failure due to having no access to package-owned declarations (ie extension functions, typealiases, top level functions). Is this a bug, or am I using the incremental compiler wrong?
https://github.com/JetBrains/kotlin/blob/master/compiler/incremental-compilation-impl/src/org/jetbrains/kotlin/incremental/IncrementalCompilerRunner.kt#L219Fatih Yalmanbas
11/29/2019, 11:23 AMinternal
? Extra points if i do not need to use an annotation to tag those classes.jdemeulenaere
12/09/2019, 12:50 PMDeclarationDescriptor
, FunctionDescriptor
, etc . Same question for BindingContext
jdemeulenaere
12/10/2019, 10:39 AMKotlinType
? It works but it is pretty slow as it fetches all top level extensions that are available (even the ones not imported). I can add restriction on the package and only get imported functions if it can help make things faster. Thanks a lot 🙂
fun getExtensionsOnType(type: KotlinType, scope: LexicalScope): List<DeclarationDescriptor> {
val extensionKindFilter = kindFilter exclude DescriptorKindExclude.NonExtensions
return scope.collectDescriptorsFiltered(extensionKindFilter, nameFilter)
.filter {
val receiverType = (it as? CallableDescriptor)?.extensionReceiverParameter?.value?.type
receiverType != null && this.isSubtypeOf(receiverType)
}
}
Sujit
12/18/2019, 4:36 AM@kotlin.Deprecated public constructor
in the class
file?raulraja
12/18/2019, 5:09 PMhttps://www.youtube.com/watch?v=n9smQNxUyBI▾
Leland Takamine
12/19/2019, 11:08 PM// Transform all instances of this call...
foo()
// ...into this via a compiler plugin:
foo("Hello")
I'm playing around with CallResolutionInterceptorExtension
but wondering if there are any other extensions I should be looking at.egorand
12/26/2019, 9:56 PMbjonnh
12/29/2019, 6:05 AMChristian Maus
12/29/2019, 11:20 AMfun <IP, R> (() -> IP).then(f: (IP) -> R): () -> R = { f(this()) }
sealed class Option<out T>
data class Some<T>(val value: T) : Option<T>()
object None : Option<Nothing>()
fun maybeNull(): String? = "abcd"
fun <T> toOption(value: T?) = if (value != null) {
Some(value)
} else {
None
}
fun toStringOption(value: String?) = value?.let { Some(value) } ?: None
//should be inferred to () -> Option<String>, but is inferred to () -> Option<String?>
val does_not_work: () -> Option<String> = ::maybeNull.then(::toOption)
//type is correct if no generic type is used
val works: () -> Option<String> = ::maybeNull.then(::toStringOption)
Marc Knaup
12/31/2019, 8:28 AMNothing?
to denote “no data” rather than Unit
?
https://github.com/JetBrains/kotlin/blob/0e4e5ac287c3836ca4434c2c6f626b9145c284e5/compiler/fir/tree/gen/org/jetbrains/kotlin/fir/visitors/FirVisitorVoid.kt#L128Rohan Maity
01/03/2020, 7:28 AMall-open
except that it would contain some extra custom pre-configured annotations for easeChristian Maus
01/12/2020, 6:18 PMsealed class Expression<A>
data class Num(val value: Int) : Expression<Int>()
data class Bool(val value: Boolean) : Expression<Boolean>()
data class Add(val a: Expression<Int>, val b: Expression<Int>) : Expression<Int>()
data class Equals<A>(val first: Expression<A>, val second: Expression<A>) : Expression<Boolean>()
fun <A> eval(e: Expression<A>): A = when (e) {
is Num -> e.value
is Bool -> e.value
is Add -> (eval(e.a) + eval(e.b))
is Equals<*> -> eval(e.first) == eval(e.second)
} as A
fun main(args: Array<String>) {
val exp = Equals(
Equals(
Num(3),
Add(
Num(1),
Num(2)
)
),
Bool(true)
)
println(eval(exp))
}
Is there a way to get rid of the cast to A?zak.taccardi
01/15/2020, 5:28 PMKoxAlen
01/24/2020, 9:57 AMimport java.lang.reflect.ParameterizedType
import java.lang.reflect.Type
import kotlin.reflect.typeOf
data class Definition(
val name: String
)
@ExperimentalStdlibApi
fun main() {
val parsed1: List<Definition> = parseGood("")
val parsed2: List<Definition> = parseBad("")
}
@ExperimentalStdlibApi
private inline fun <reified T> parseGood(blob: String): T {
return readValue(blob)
}
@ExperimentalStdlibApi
private inline fun <reified T> parseBad(blob: String): List<T> {
return readValue(blob)
}
@ExperimentalStdlibApi
private inline fun <reified T> readValue(blob: String): T {
println(typeOf<T>())
val type = object : TypeHolder<T>() {}.type
println(type)
//Do magic with type and return a T
return listOf(Definition("foo")) as T
}
open class TypeHolder<T> protected constructor() {
private var _type: Type
val type get() = _type
init {
val superClass = javaClass.genericSuperclass
_type = (superClass as ParameterizedType).actualTypeArguments[0]
}
}
This prints
kotlin.collections.List<Definition>
java.util.List<? extends Definition>
kotlin.collections.List<Definition>
java.util.List<? extends T>
So for typeOf
T is the same in both cases but for the subclass generation one has the signature with the real type and the other one doesn't.
Is this a bug, or I'm missing something?charlesmuchene
01/27/2020, 9:22 AMself-referential
vararg parameter passed on the constructor is a container and thus the given class is constructible
?
Current fix: Requires I make the parameter type nullable or otherwise suppress this warning.mp
01/27/2020, 7:29 PMKiryushin Andrey
01/28/2020, 9:01 AM.?
) in Kotlin vs the similar operator in C#. Say we have such classes
data class Head(val name: String)
data class Department(val head: Head)
data class Employee(val department: Department)
and the variable e
of type Employee?
.
Kotlin will refuse to compile an expression e?.department.head.name
complaining that head
property cannot be accessed on an instance of Department?
type. So this expression needs to be rewritten as e?.department?.head?.name
, which in my opinion looks a bit clumsy cause we know in advance that the only thing in this chain that can possibly be null is e
.
In C#, on the other hand, this operator discards the whole chain if null is encountered, compiling e?.Department.Head.Name
to (e != null) ? e.Department.Head.Name : null
.
Am I missing something or does C# really do a better job in this case? Is this difference in behavior due to the fact that it was hard or impossible to to implement a C#-like behavior given null-aware type system? Or maybe there are some other cases when this Kotlin compiler behavior allows for safer code and therefore is desired? Would like to hear the community as well as JetBrains folks' thoughts on this.jimn
01/28/2020, 11:12 AMe: org.jetbrains.kotlin.codegen.CompilationException: Back-end (JVM) Internal error: wrong bytecode generated
jimn
01/28/2020, 11:19 AMjimn
01/28/2020, 11:19 AMdmitriy.novozhilov
01/28/2020, 11:20 AMjimn
01/28/2020, 11:22 AMdmitriy.novozhilov
01/28/2020, 11:26 AMjimn
01/28/2020, 7:08 PM