constructor-reference

How to pass a type parameter to a generic class constructor reference?

时光总嘲笑我的痴心妄想 提交于 2019-12-06 03:45:48
问题 Assume the following code: class ConstructMe<T> {} data class Test<T> constructor(var supplier: () -> ConstructMe<T>) {} fun main(args: Array<String>) { works<Int>() breaks<Int>() } fun <T> works() { Test<T>({ ConstructMe<T>() }) // (1) any one class type parameter can be removed like: Test({ ConstructMe<T>() }) // (2) still works (class type inferred by argument type) Test<T>({ ConstructMe() }) // (3) still works (argument type inferred by class type) } fun <T> breaks() { Test<T>(:

Constructor reference for inner class fails with VerifyError at runtime

廉价感情. 提交于 2019-12-03 11:33:21
问题 I am creating a supplier for an inner class constructor using the lambda ctx -> new SpectatorSwitcher(ctx) . IntelliJ suggested that I change it to SpectatorSwitcher::new instead. SpectatorSwitcher is a non-static inner class of the class I'm working in. The suggested code compiles fine (using maven) but I get the following VerifyError on execution: Exception in thread "main" java.lang.VerifyError: Bad type on operand stack Exception Details: Location: Test.lambda$runTest$8(LTest$Worker;)V @2

Invalid constructor reference when using local class?

耗尽温柔 提交于 2019-12-03 07:32:41
问题 Given the following code: package com.gmail.oksandum.test; import java.util.ArrayList; import java.util.List; public class Test { public static void main(String[] args) { } public void foo() { class LocalFoo { LocalFoo(String in) { //Some logic } } List<String> ls = new ArrayList<>(); ls.stream().map(LocalFoo::new); //Line 21 } } my IDE gives me no errors. That is, until I try to build the project and run it. When I do that it gives me a compiler error that looks like this: Error:(21, 24)

Constructor reference for inner class fails with VerifyError at runtime

旧街凉风 提交于 2019-12-03 01:54:20
I am creating a supplier for an inner class constructor using the lambda ctx -> new SpectatorSwitcher(ctx) . IntelliJ suggested that I change it to SpectatorSwitcher::new instead. SpectatorSwitcher is a non-static inner class of the class I'm working in. The suggested code compiles fine (using maven) but I get the following VerifyError on execution: Exception in thread "main" java.lang.VerifyError: Bad type on operand stack Exception Details: Location: Test.lambda$runTest$8(LTest$Worker;)V @2: invokedynamic Reason: Type 'Test$Worker' (current frame, stack[1]) is not assignable to 'Test'

Are there constructor references in Kotlin?

最后都变了- 提交于 2019-11-28 07:07:16
In Java we have the Class::new syntax for constructor references. I know, there are callable references for methods, but how about constructors? A typical use case for me would be factories. Ilya Ryzhenkov You can get a functional instance for constructor by simply using ::ClassName , as if it were factory function. 来源: https://stackoverflow.com/questions/26472839/are-there-constructor-references-in-kotlin

Are there constructor references in Kotlin?

对着背影说爱祢 提交于 2019-11-27 01:26:04
问题 In Java we have the Class::new syntax for constructor references. I know, there are callable references for methods, but how about constructors? A typical use case for me would be factories. 回答1: You can get a functional instance for constructor by simply using ::ClassName , as if it were factory function. 来源: https://stackoverflow.com/questions/26472839/are-there-constructor-references-in-kotlin