我可以写:
@IdRes
abstract fun getHeaderId(): Int
Run Code Online (Sandbox Code Playgroud)
用val而不是funkotlin?它抱怨我写作时需要一个支持领域或代表:
@IdRes <-- errors
abstract val headerId: Int
Run Code Online (Sandbox Code Playgroud)
在这种情况下哪个是最惯用的?单线fun或带有背景场的混乱(我不习惯支持场,也许它是变化阻力,我从来没有真正使用它们所以我认为它们是不愉快的)
是否可以在ConstrainLayout中将两个textview约束成这样,以便它们在需要时占据每个水平空间的50%,如果没有,则占据另一个水平空间。
C正确:平均分配水平空间并垂直增长
D错误:右侧textView是以左侧textView为代价而增长的。
E错误:正确的textView垂直增长,而不是填充可用的水平空间。
我已经尝试了很多东西。它认为layout_width="wrap_content"还是layout_constraintWidth_default="wrap"必须的,因为它是textViews传达所需空间的唯一方法。
实验过:
layout_constrainedWidth="true/false"
layout_constraintWidth_default="spread/wrap"
layout_constraintWidth_max="wrap"
layout_constraintHorizontal_weight="1" // with 0dp as width
Run Code Online (Sandbox Code Playgroud)
大多数情况下,在水平链中使用textViews进行实验,因为该问题存在对称性,所以我认为对称解决方案是有意义的。
有人管理过这个吗,还是不可能?我不知道为什么它会像链中的第二个元素“重”于第一个元素那样“强迫”它,使其成为唯一垂直生长的元素。
我正在尝试在 kotlin 中定义一个 StringDef:
@Retention(AnnotationRetention.SOURCE)
@StringDef(NORTH, SOUTH)
annotation class FilterType {
companion object {
const val NORTH = "NORTH"
const val SOUTH = "SOUTH"
}
}
Run Code Online (Sandbox Code Playgroud)
我认为上面的代码有问题。
// I can send anything to this method, when using my kotlin stringDef
private fun takeString(@DirectionJava.Direction filterType: String) {
Run Code Online (Sandbox Code Playgroud)
我想要与下面的java等效的kotlin:
public class DirectionJava {
public static final String NORTH = "NORTH";
public static final String SOUTH = "SOUTH";
@Retention(RetentionPolicy.SOURCE)
@StringDef({
NORTH,
SOUTH,
})
public @interface Direction {
}
}
Run Code Online (Sandbox Code Playgroud)
从 kotlin 调用 java …
今天早上我带着这个警报回到我的电脑,看到图像(下面的窗口是上面一个条目的属性):
我该如何调查这个?我可以使用提供的哈希来查看它是否是误报?有人遇到过这种情况么?
值得注意的是,我最近没有改变任何关于android框架的东西.
下面是一个来自 Android 的模式示例(只是一个例子,对 android 细节不感兴趣):
/*Im a kotlin file*/
class ListItemViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
val text: = itemView.my_view
}
Run Code Online (Sandbox Code Playgroud)
然后模式是您访问文本字段,如下所示:
/*Im a Java file*/
holder.text.setText("Metasyntactic variable");
Run Code Online (Sandbox Code Playgroud)
不幸的是,有一个带有固定结构的大文件执行上述操作,然后有:
/*Im a Java file, but this particular holder is a kotlin file*/
holder.getText().setText("Metasyntactic variable");
Run Code Online (Sandbox Code Playgroud)
有没有可能解决这个问题?也许有一些@Jvm注释
在 5.2.9 spring 项目中
<mvc:resources location="/, classpath:/META-INF/web-resources/" mapping="/resources/**"/>
Run Code Online (Sandbox Code Playgroud)
在 webmvc-config.xml 中。静态图像与标题一起提供cache-control: /*omissions */ no-cache。我正在尝试覆盖特定文件或文件夹的此标头。
<mvc:resources location="/, classpath:/META-INF/web-resources/" mapping="/resources/**">
<mvc:cache-control max-age="3600" cache-public="true"/>
</mvc:resources>
Run Code Online (Sandbox Code Playgroud)
如果我替换为上面的内容,它会按我想要的方式工作,但它针对所有文件。我尝试更具体一些,例如:
<mvc:resources location="/, classpath:/META-INF/web-resources/" mapping="/resources/**"/>
<mvc:resources location="/, classpath:/META-INF/web-resources/" mapping="/resources/myImages/**">
<mvc:cache-control max-age="3600" cache-public="true"/>
</mvc:resources>
Run Code Online (Sandbox Code Playgroud)
但我不认为它喜欢有两个资源标签?我在文档中没有看到明确的方法来执行我想要的操作,但我对该领域不太熟悉。我不认为我想要一个“拦截器”?
昨天我读了一些关于整个 monad 的内容。我绝不掌握它,但它不会阻止我尝试使用它。这个问题中的一些答案确实推动了 monad 的存在,让事情变得更好、更具可读性。“Haskell 对 monad 有很好的支持”,这是否意味着我可以编写很好的代码,例如开箱即用?
或者这是否意味着我可以为可能的类型编写代码,从而使其易于使用。
我在做代码出现的时候遇到了如下问题:
将两个 Maybe 中的值加 1,然后将它们相乘。
在阅读了有关乘以两个可能的问题并查看了类型分类百科之后,我得出了以下结论(经过一番认真的挠头之后):
let test = Just (*) <*> ((+1) <$> marker1Pos) <*> ((+1) <$> marker2Pos)
Run Code Online (Sandbox Code Playgroud)
它有效,但对我来说看起来不太理想。
我可以使用我在第一个链接中读到的 do-block 来完成此操作吗?
我给Kotlin一个机会; 内容编码,我有一个字符的ArrayList,我想根据括号的匹配方式进行分类:
(abcde) // ok characters other than brackets can go anywhere
)abcde( // ok matching the brackets 'invertedly' are ok
(({()})) // ok
)()()([] // ok
([)] // bad can't have different overlapping bracket pairs
((((( // bad all brackets need to have a match
Run Code Online (Sandbox Code Playgroud)
我的解决方案出来了(递归):
//charList is a property
//Recursion starter'upper
private fun classifyListOfCharacters() : Boolean{
var j = 0
while (j < charList.size ) {
if (charList[j].isBracket()){
j = checkMatchingBrackets(j+1, charList[j])
}
j++
}
return j == …Run Code Online (Sandbox Code Playgroud) 我目前必须写
val myList: List<Int>? = listOf()
if(!myList.isNullOrEmpty()){
// myList manipulations
}
Run Code Online (Sandbox Code Playgroud)
哪个智能广播myList不能为非null。以下不提供任何智能广播:
if(!myList.orEmpty().isNotEmpty()){
// Compiler thinks myList can be null here
// But this is not what I want either, I want the extension fun below
}
if(myList.isNotEmptyExtension()){
// Compiler thinks myList can be null here
}
private fun <T> Collection<T>?.isNotEmptyExtension() : Boolean {
return !this.isNullOrEmpty()
}
Run Code Online (Sandbox Code Playgroud)
有没有办法为自定义扩展获取smartCast?
我经常以这样的数据源结束(下面的伪代码,没有任何特定的语法,只是为了说明):
list = {
"XLabel",
"XDescription",
"YLabel",
"YDescription",
"ZLabel",
"ZDescription"
}
Run Code Online (Sandbox Code Playgroud)
所需的输出是:
list = {
MyClass("XLabel", "XDescription"),
MyClass("YLabel", "YDescription"),
MyClass("ZLabel", "ZDescription")
}
Run Code Online (Sandbox Code Playgroud)
有什么比做 a 更干净fold(),然后把它折叠成一个新列表吗?我也拒绝做一些奇怪的事情list.partition().zip()
我基本上想要一个更强大的功能map,就像mapChunks( it1, it2 -> MyClass(it1, it2))分块是功能的一部分一样,这样它就会变得简单和漂亮。(我的示例将列表分成两个块,但 3 也是一个普遍的用例。)
有这个功能吗?或者最惯用的方法是什么?
我有一个包装数据对象的类。我想要
class SomeWrappingClass {
lateinit var data: SomeDataHolder
// Want to 'forward' many properties to the data class
var thing0: String? by data
var thing1: String? by data
var thing2: String? by data
}
Run Code Online (Sandbox Code Playgroud)
或者更现实地说:
var thing0: String? by data.thing0
Run Code Online (Sandbox Code Playgroud)
下面的函数可以用它来实现,但据我了解,它们使用反射,我想保持 SomeDataHolder 清晰。我还没有尝试过反射,但我看到它们要么变得很大,要么整个类变成一个键值映射。
SomeDataHolder {
operator fun getValue(internationalTransferViewModel: SomeWrappingClass, property: KProperty<*>): String? {
TODO("not implemented")
}
operator fun setValue(internationalTransferViewModel: SomeWrappingClass, property: KProperty<*>, s: String?) {
TODO("not implemented")
}
Run Code Online (Sandbox Code Playgroud)
所以基本上有没有更短的方法来写这个:
var thing: String?
get() = data.thing
set(value) {
data.thing = value …Run Code Online (Sandbox Code Playgroud) kotlin ×7
android ×4
admob ×1
annotations ×1
haskell ×1
parsing ×1
spring ×1
spring-mvc ×1
symantec ×1
trojan ×1