Hi Andrey,
thanks for your reply. Think I wrote some fabulous code now that made the compiler break: Compiler terminated with exit code: 2. It is not a compiler error concerning my code, but something inside jetbrains code generation seems to break (judging from the stack trace below). Here is what I did which in this first step still compiles and runs fine:
private inline fun <T> Collection<T>.newInstanceNotNull() : Collection<T>
{
val newInstance = javaClass.newInstance()
if(newInstance != null)
return newInstance
return ArrayList<T>
}
public inline fun <T> Collection<T>.select(fn : (T) -> Boolean) : Collection<T>
{
val result = newInstanceNotNull()
for (item in this) {
if (fn(item))
result.add(item)
}
return result
}
fun main(args : Array<String>)
{
val list = ArrayList<Int>()
list.add(3)
list.add(7)
val set = HashSet<Int>()
set.add(3)
set.add(7)
var resultList : List<Int> = (list.select{ it < 12}).toList()
println(resultList)
var resultSet : Set<Int> = (set.select{ it < 12}).toSet()
println(resultSet)
}
Then I thought that this toList and toSet conversion is not too bloody elegenat. So I added these two methods:
public inline fun <T> Set<T>.select(fn : (T) -> Boolean) : Set<T>
{
return super.select(fn)
}
public inline fun <T> List<T>.select(fn : (T) -> Boolean) : List<T>
{
return super.select(fn)
}
The test code then becomes:
fun main(args : Array<String>)
{
val list = ArrayList<Int>()
list.add(3)
list.add(7)
val set = HashSet<Int>()
set.add(3)
set.add(7)
var resultList : List<Int> = list.select{ it < 12}
println(resultList)
var resultSet : Set<Int> = set.select{ it < 12}
println(resultSet)
}
Note, there is no more toList() nor toSet(). When I rebuild the project I get this exception dump (after waiting for quite a while):
java.lang.IllegalStateException: Internal error: (34,18) java.lang.NullPointerException
@BindingContext.java:144
at org.jetbrains.jet.codegen.CompilationErrorHandler$1.reportException(CompilationErrorHandler.java:27)
at org.jetbrains.jet.codegen.GenerationState.compileCorrectFiles(GenerationState.java:120)
at org.jetbrains.jet.compiler.CompileSession.generate(CompileSession.java:161)
at org.jetbrains.jet.compiler.CompileEnvironment.compileModule(CompileEnvironment.java:156)
at org.jetbrains.jet.compiler.CompileEnvironment.compileModuleScript(CompileEnvironment.java:107)
at org.jetbrains.jet.cli.KotlinCompiler.exec(KotlinCompiler.java:144)
at org.jetbrains.jet.cli.KotlinCompiler.exec(KotlinCompiler.java:90)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.jetbrains.jet.plugin.compiler.JetCompiler.execInProcess(JetCompiler.java:276)
at org.jetbrains.jet.plugin.compiler.JetCompiler.runInProcess(JetCompiler.java:243)
at org.jetbrains.jet.plugin.compiler.JetCompiler.doCompile(JetCompiler.java:148)
at org.jetbrains.jet.plugin.compiler.JetCompiler.compile(JetCompiler.java:104)
at com.intellij.compiler.impl.CompileDriver.compileSources(CompileDriver.java:1931)
at com.intellij.compiler.impl.CompileDriver.translate(CompileDriver.java:1254)
at com.intellij.compiler.impl.CompileDriver.doCompile(CompileDriver.java:986)
at com.intellij.compiler.impl.CompileDriver.doCompile(CompileDriver.java:747)
at com.intellij.compiler.impl.CompileDriver.access$1000(CompileDriver.java:104)
at com.intellij.compiler.impl.CompileDriver$8.run(CompileDriver.java:665)
at com.intellij.compiler.progress.CompilerTask.run(CompilerTask.java:155)
at com.intellij.openapi.progress.impl.ProgressManagerImpl$TaskRunnable.run(ProgressManagerImpl.java:469)
at com.intellij.openapi.progress.impl.ProgressManagerImpl$2.run(ProgressManagerImpl.java:178)
at com.intellij.openapi.progress.impl.ProgressManagerImpl.executeProcessUnderProgress(ProgressManagerImpl.java:218)
at com.intellij.openapi.progress.impl.ProgressManagerImpl.runProcess(ProgressManagerImpl.java:169)
at com.intellij.openapi.progress.impl.ProgressManagerImpl$8.run(ProgressManagerImpl.java:378)
at com.intellij.openapi.application.impl.ApplicationImpl$6.run(ApplicationImpl.java:434)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:441)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:303)
at java.util.concurrent.FutureTask.run(FutureTask.java:138)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
at java.lang.Thread.run(Thread.java:662)
at com.intellij.openapi.application.impl.ApplicationImpl$1$1.run(ApplicationImpl.java:145)
Caused by: Internal error: (34,18) java.lang.NullPointerException
@BindingContext.java:144
at org.jetbrains.jet.codegen.ExpressionCodegen.genQualified(ExpressionCodegen.java:156)
at org.jetbrains.jet.codegen.ExpressionCodegen.visitDotQualifiedExpression(ExpressionCodegen.java:1569)
at org.jetbrains.jet.codegen.ExpressionCodegen.visitDotQualifiedExpression(ExpressionCodegen.java:58)
at org.jetbrains.jet.lang.psi.JetDotQualifiedExpression.accept(JetDotQualifiedExpression.java:37)
at org.jetbrains.jet.codegen.ExpressionCodegen.genQualified(ExpressionCodegen.java:147)
at org.jetbrains.jet.codegen.ExpressionCodegen.gen(ExpressionCodegen.java:161)
at org.jetbrains.jet.codegen.ExpressionCodegen.gen(ExpressionCodegen.java:165)
at org.jetbrains.jet.codegen.ExpressionCodegen.visitReturnExpression(ExpressionCodegen.java:925)
at org.jetbrains.jet.codegen.ExpressionCodegen.visitReturnExpression(ExpressionCodegen.java:58)
at org.jetbrains.jet.lang.psi.JetReturnExpression.accept(JetReturnExpression.java:38)
at org.jetbrains.jet.codegen.ExpressionCodegen.genQualified(ExpressionCodegen.java:147)
at org.jetbrains.jet.codegen.ExpressionCodegen.gen(ExpressionCodegen.java:161)
at org.jetbrains.jet.codegen.ExpressionCodegen.generateBlock(ExpressionCodegen.java:853)
at org.jetbrains.jet.codegen.ExpressionCodegen.visitBlockExpression(ExpressionCodegen.java:722)
at org.jetbrains.jet.codegen.ExpressionCodegen.visitBlockExpression(ExpressionCodegen.java:58)
at org.jetbrains.jet.lang.psi.JetBlockExpression.accept(JetBlockExpression.java:45)
at org.jetbrains.jet.codegen.ExpressionCodegen.genQualified(ExpressionCodegen.java:147)
at org.jetbrains.jet.codegen.ExpressionCodegen.gen(ExpressionCodegen.java:161)
at org.jetbrains.jet.codegen.ExpressionCodegen.returnExpression(ExpressionCodegen.java:936)
at org.jetbrains.jet.codegen.FunctionCodegen.generatedMethod(FunctionCodegen.java:232)
at org.jetbrains.jet.codegen.FunctionCodegen.generateMethod(FunctionCodegen.java:73)
at org.jetbrains.jet.codegen.FunctionCodegen.gen(FunctionCodegen.java:63)
at org.jetbrains.jet.codegen.NamespaceCodegen.generate(NamespaceCodegen.java:71)
at org.jetbrains.jet.codegen.GenerationState.generateNamespace(GenerationState.java:131)
at org.jetbrains.jet.codegen.GenerationState.compileCorrectFiles(GenerationState.java:114)
… 33 more
Caused by: java.lang.NullPointerException
at org.jetbrains.jet.lang.resolve.BindingContext$3.normalize(BindingContext.java:144)
at org.jetbrains.jet.lang.resolve.BindingContext$3.normalize(BindingContext.java:125)
at org.jetbrains.jet.util.slicedmap.Slices$SliceWithOpposite.makeKey(Slices.java:176)
at org.jetbrains.jet.util.slicedmap.SlicedMapImpl.get(SlicedMapImpl.java:85)
at org.jetbrains.jet.lang.resolve.BindingTraceContext.get(BindingTraceContext.java:79)
at org.jetbrains.jet.lang.resolve.BindingTraceContext$1.get(BindingTraceContext.java:47)
at org.jetbrains.jet.codegen.ExpressionCodegen.generateThisOrOuter(ExpressionCodegen.java:1436)
at org.jetbrains.jet.codegen.StackValue$ThisOuter.put(StackValue.java:1018)
at org.jetbrains.jet.codegen.StackValue$CallReceiver.genReceiver(StackValue.java:1155)
at org.jetbrains.jet.codegen.StackValue$CallReceiver.put(StackValue.java:1133)
at org.jetbrains.jet.codegen.ExpressionCodegen.invokeMethodWithArguments(ExpressionCodegen.java:1353)
at org.jetbrains.jet.codegen.ExpressionCodegen.invokeFunction(ExpressionCodegen.java:1289)
at org.jetbrains.jet.codegen.ExpressionCodegen.visitCallExpression(ExpressionCodegen.java:1255)
at org.jetbrains.jet.codegen.ExpressionCodegen.visitCallExpression(ExpressionCodegen.java:58)
at org.jetbrains.jet.lang.psi.JetCallExpression.accept(JetCallExpression.java:45)
at org.jetbrains.jet.codegen.ExpressionCodegen.genQualified(ExpressionCodegen.java:147)
… 57 more
I was a bit surprised that f.ex.
public inline fun <T> Set<T>.select(fn : (T) -> Boolean) : Set<T>
{
return super.select(fn)
}
compiled fine. I thought I would have to write something like this to make it compile:
public inline fun <T> Set<T>.select(fn : (T) -> Boolean) : Set<T>
{
return super.select(fn).toSet()
}
with toSet being:
public inline fun <T> Set<T>.toSet() : Set<T>
{
return this
}
Anyway, I get the same compiler dump with this approach as well. Maybe someone could spare a little time to have a look into this. Would be nice if the old Smalltalk yourself trick (method returns self as in the method just above this paragraph) could be applied here.
Kind regards, Oliver