Tag: generics

Kotlin通用属性问题

在将我的Android项目从Java转换为Kotlin时,我遇到了Kotlin的一些问题。 假设我有接口I和扩展接口I的接口O. interface I{ } interface O: I{ } 通用类A具有扩展接口I的通用参数V,扩展类A的通用类B: abstract class A { } class B : A() { } 当我试图创建这样的属性: val returnB: A get() = b 我得到编译器错误“需要A,findB”。 在Java中,这将工作没有任何问题。 我怎样才能使用Kotlin访问? 我需要在我的应用程序中使用这种方法的基本类。 具有Navigator类的generics参数的BaseViewModel: abstract class BaseViewModel(application: Application, val repositoryProvider: RepositoryProvider) : AndroidViewModel(application) { var navigator: N? = null fun onDestroyView() { navigator = null } […]

Kotlin实体types参数不智能投射

我正在试验设置未初始化的值,并试图让以下工作。 这主要是对物化仿制的权力(和限制)的好奇心。 我试图为数据类的可选参数提供默认值。 inline fun uninitialized(): T = when (T::class) { Long::class -> -1L // Type mismatch. Required: T Found: Long String::class -> “” // Type mismatch. Required: T Found: String // and so on… else -> throw UnsupportedOperationException(“No uninitialized value defined for ” + T::class) } data class Thing( var id: Long = uninitialized(), […]

Koltingenerics声明 – 站点方差构造

我正在阅读kotlin没有通配符的原因( https://kotlinlang.org/docs/reference/generics.htm l)。 这一切都来到声明网站的变化。 我们有和结构应该取代通配符。 我想我明白了是如何运作的,但我却在遇到麻烦。 所以在java中,我们可以这样写: public List list1; public List list2; 初始化后的第一种情况变成只读列表(虽然不是完全不可改变的原因,我们可以清除它),如果我们把每个元素都视为数字,就可以读取它们。 第二种情况是只写(尽管我们可以阅读它,如果我们把每个元素作为对象)。 我们可以写那里字符串和它的子类。 在Kotlin中,我能够像这样使用重新创建list1示例: class Service { val container = Container(mutableListOf(“1”, “2”, “3”)) } class Container(var list1: MutableList) 最后我尝试了一些类似于东西,认为我可以重新创建list2的例子,但是我失败了: 有人可以向我解释如何在Kotlin中实现我的list2例子吗? 我应该如何正确使用结构?

Kotlin覆盖子types的乐趣

我无法inheritance包含一个基types的方法/乐趣的接口,我想重写作为实现它的类中的子types。 到目前为止,我有界面 interface IModel { fun convert(dataModel: BaseDataModel) } 而实施它的class级: class SettingsModel: IModel { override fun convert(dataModel: BaseDataModel) { // Conversion of models here } } 而且我也有SettingsDataModel这是: class SettingsDataModel: BaseDataModel() { } 我想要实现的是每个类/模型实现IModel,能够得到具体的DataModel,如: class SettingsModel: IModel { override fun convert(dataModel: SettingsDataModel) { // Conversion of models here } } 而不需要施放它。 我想我不能,因为它修改了有趣的签名,使它不是一个真正的重写。 我尝试使用generics和通用约束,但没有运气: interface IModel { […]

Javatypes作为GSON的参数

在GSON中获取你所做的对象列表 Gson gson = new Gson(); Type token = new TypeToken<List>(){}.getType(); return gson.fromJson(json, token); 它工作的很好,但我想走得更远,并有MyType参数化,所以我可以有一个共同的function来解析与此代码的对象的列表 // the common function public List fromJSonList(String json, Class type) { Gson gson = new Gson(); Type collectionType = new TypeToken<List>(){}.getType(); return gson.fromJson(json, collectionType); } // the call List myTypes = parser.fromJSonList(jsonString, MyType.class); 可悲的是返回一个StringMaps数组,而不是types。 T被解释为另一种generics,而不是我的types。 任何解决方法?

我必须分配什么编译时types才能获得java.util.Map的返回值

我想从另一个字体派生出一个字体。 这是我想要做的: val font : Font = this.label.getFont(); val attributes : Map = font.getAttributes(); attributes.put(TextAttribute.UNDERLINE, TextAttribute.UNDERLINE_ON); this.label.setFont(font.deriveFont(attributes)); 但是,Kotlin编译器在这一行上抱怨: val attributes : Map = font.getAttributes(); 随着消息: Type mismatch: inferred type is (Mutable)Map! but Map was expected 根据我对Javagenerics的有限理解,我知道font.getAttributes()返回一个java.util.Map ; 后一种types的参数意味着当你从这个映射中创建一个有界/关闭的genericstypes时,请指定作为第二个types参数的任何扩展java.lang.Object的东西 。 所以,当我首先尝试了以下几行: val attributes : java.util.Map = font.getAttributes(); Kotlin编译器说: 这个类不应该在Kotlin中使用。 改为使用kotlin.collections.Map或kotlin.collections.MutableMap。 还说: Type mismatch: inferred type is […]

这个错误是什么意思,当从java代码转换到通用的kotlin,单T后禁止?

试图将一些java代码转换为kotlin,并与genericstypes的问题。 这里的一个示例是Danny Preussler的Bundles和ViewModel的post的Java代码: 这里 class BundleAwareViewModelFactory implements ViewModelProvider.Factory { private final Bundle bundle; private final ViewModelProvider.Factory provider; public BundleAwareViewModelFactory(@Nullable Bundle bundle, ViewModelProvider.Factory provider) { this.bundle = bundle; this.provider = provider; } @SuppressWarnings(“unchecked”) @Override public T create(final Class modelClass) { T viewModel = (T) provider.create(modelClass); if (bundle != null) { viewModel.readFrom(bundle); } return viewModel; } } […]

无法使用kotlinx.serialization将Json解析为数据类

在Android项目中,我试图使用kotlinx.serialization和Retrofit从远程端点解析Json响应。 我在使用Jake Warthon的https://github.com/JakeWharton/retrofit2-kotlinx-serialization-converter来设置转换器时设置改造实例 Retrofit.Builder() .baseUrl(baseUrl) .addConverterFactory(stringBased(contentType, ::parse, ::stringify)) .addCallAdapterFactory(CoroutineCallAdapterFactory()) .build() 我有以下模型注解@Serializable,我想用来解析Json响应 import kotlinx.serialization.Serializable @Serializable data class Response(val groups: List<GroupsResponse>) 请注意,Response和嵌套的GroupsResponse都定义了一个通用types。 我一直在浏览文档,但是我一直无法理解generics和kotlinx.serialization是如何一起玩的,尽管它提到它支持generics,并且您应该使用生成的序列化方法访问那些模型的序列化程序 。 我已经建立了项目使用正确的插件按照kotlinx.serialization和Jake的库的文档,但问题是,该项目甚至不编译,因为我正在得到以下types的错误,当构建过程执行kaptDebugKotlin gradle任务 e: /Users/nico/Dev/git/edreams/workshops/K-Places/data/build/tmp/kapt3/stubs/debug/com/edreams/android/workshops/kotlin/data/venues/remote/response/Response.java:75: error: non-static type variable T cannot be referenced from a static context public static final class $serializer implements kotlinx.serialization.KSerializer<com.edreams.android.workshops.kotlin.data.venues.remote.response.Response> { e: /Users/nico/Dev/git/edreams/workshops/K-Places/data/build/tmp/kapt3/stubs/debug/com/edreams/android/workshops/kotlin/data/venues/remote/response/GroupsResponse.java:109: error: cannot find symbol kotlinx.serialization.KSerializer typeSerial0) { […]

Spring在kotlin中注入通用接口实现列表

免责声明:Kotlin新手,可能是一个容易解决的问题或误解的基础知识。 我正在试图注入一个特定的接口的Spring实现列表,在一个普通的Java类中,这很容易,就像这样: @Autowired List myClassList; 但是在Kotlin做以下给我一个错误 @Autowired lateinit private var myClassList: List<IMyClass> // No beans of ‘? extends IMyClass’ or ‘List<? extends IMyClass>’ types found 这样做: @Autowired lateinit private var myClassList: List<IMyClass> 使注入工作,但不允许我使用接口中定义的函数,该函数将通用对象作为输入 // Out-projected type ‘IMyClass’ prohibits the use of ‘public abstract fun myFunction(obj: T!): T! defined in com.path.IMyClass’ 那么我该如何解决呢? 用一些特定的syntac来重写Kotlin中的接口更容易吗?

Kotlingenerics方法和inheritance

在我的代码中,我想在抽象类中创建一个方法,该方法返回一些Observable。 然后这个抽象类的实现将返回某些(指定)types的Observable。 不幸的是,Android Studio会在实现方法()中返回一个错误“types不匹配”: 预计:可观察 find:Observable 我的MockDrawerList.getList()返回Observable 请关注execute()和buildUseCaseObservable 抽象类 public abstract class UseCase(threadExecutor: ThreadExecutor, postExecutionThread: PostExecutionThread) { private val threadExecutor: ThreadExecutor private val postExecutionThread: PostExecutionThread private var subscription: Subscription = Subscriptions.empty() init { this.postExecutionThread = postExecutionThread this.threadExecutor = threadExecutor } protected abstract fun buildUseCaseObservable(): Observable public fun execute(useCaseSubsriber: Subscriber) { subscription = buildUseCaseObservable() .subscribeOn(Schedulers.from(threadExecutor)) .observeOn(postExecutionThread.getScheduler()) […]