在Kotlin有:
val - readonly财产const val - 编译时常量从文档:
编译时常数
在编译时已知的值的属性可以使用
const修饰符标记为编译时常量.这些属性需要满足以下要求:
- 顶级或对象的成员
- 使用String类型或基本类型初始化
- 没有自定义的吸气剂
鉴于kotlin编译器确实知道识别初始化值(例如,不需要在初始化程序中定义变量类型):
const修饰符本身吗?如何测试KType变量是否包含可为空的kotlin类型的值(eG Int?)?
我有
var type: KType
Run Code Online (Sandbox Code Playgroud)
变量来自a KProperty<*>.returnType,我需要检测它是否等于某些kotlin类型(Int,Long等).这适用于:
when (type) {
Int::class.defaultType -> ...
Long::class.defaultType -> ...
else -> ...
}
Run Code Online (Sandbox Code Playgroud)
但这仅适用于非可空类型,因此第一个分支与Int不匹配?但是我还是无法弄清楚我是如何检测到的类型是Int?除了明显但不那么好
type.toString().equals("kotlin.Int?")
Run Code Online (Sandbox Code Playgroud) 在Kotlin中,此代码的语法是否更短:
if(swipeView == null){
swipeView = view.find<MeasureTypePieChart>(R.id.swipeableView)
}
Run Code Online (Sandbox Code Playgroud)
首先我试过这个:
swipeView ?: view.find<MeasureTypePieChart>(R.id.swipeableView)
Run Code Online (Sandbox Code Playgroud)
但后来我意识到这不是一个任务,所以代码什么都不做.然后我试过:
swipeView = swipeView ?: view.find<MeasureTypePieChart>(R.id.swipeableView)
Run Code Online (Sandbox Code Playgroud)
哪个有效,但有点冗长.我希望这样的事情:
swipeView ?= view.find<MeasureTypePieChart>
Run Code Online (Sandbox Code Playgroud)
但不幸的是,这不起作用.有没有办法用短语法完成这个?
我知道我可以这样做:
variable?.let { it = something } which works.
Run Code Online (Sandbox Code Playgroud) 由于Kotlin有非空的断言,我发现了一些有趣的东西......
val myvar: String = null!!
Run Code Online (Sandbox Code Playgroud)
它会崩溃.
但重点是,它不会在编译时检查.
该应用程序将在运行时崩溃.
它不应该抛出编译时错误吗?
我不知道如何将 'profile' 或 'auth' props 传递给 firestoreConnect() 函数,以便根据用户 ID 或其他一些键查询集合或文档(我的代码片段如下)。
我可以在我的 jsx 代码中访问 'auth' 和 'profile' 对象,但我找不到在 firestoreConnect() 函数中使用它们的方法(我收到一个错误:)TypeError: Cannot read property 'uid' of undefined。如果我对一些 uid 进行硬编码,则一切正常,但我无法将其作为 props 值传递。
任何建议都非常感谢!
export default compose(
firebaseConnect(),
firestoreConnect(props => [{ collection: 'projects', where: [['uid', '==', props.auth.uid]] }]), <<--THIS LINE DOES NOT WORK
connect((state, props) => ({
projects: state.firestore.ordered.projects,
profile: state.firebase.profile,
auth: state.firebase.auth,
}))
)(Projects);
Run Code Online (Sandbox Code Playgroud) 出于可读性原因,我可能会破坏代码。所以
async coro_top():
print('top')
print('1')
# ... More asyncio code
print('2')
# ... More asyncio code
Run Code Online (Sandbox Code Playgroud)
...变成类似
async coro_top():
print('top')
await coro_1()
await coro_2()
async coro_1()
print('1')
# ... More asyncio code
async coro_2()
print('2')
# ... More asyncio code
Run Code Online (Sandbox Code Playgroud)
但是,额外的awaits表示它们并不严格等效
另一个并发任务可以在print('top')和之间运行代码print('1'),因此对于某些算法,竞争条件更有可能出现。
产生事件循环的开销(大概)
因此,有没有一种方法可以在不产生事件循环的情况下调用协程以避免上述情况发生?
我试图获取将两个int数组相乘的答案(输出也是int数组)。
例如,num1 = [2, 2, 0], num2 = [1, 0]会给我们[2, 2, 0, 0]
我试过的是
def multiply(num1, num2):
if num1 == [0] or num2 == [0]:
return [0]
sign = -1 if (num1[0] < 0) ^ (num2[0] < 0) else 1
num1[0] = abs(num1[0])
num2[0] = abs(num2[0])
res = [0] * (len(num1) + len(num2) + 1) # space O(n + m)
for i in range(len(num1) - 1, -1, -1):
for j in range(len(num2) - 1, -1, -1):
res[i …Run Code Online (Sandbox Code Playgroud) 如何避免使用!!类的可选属性
class PostDetailsActivity {
private var post: Post? = null
fun test() {
if (post != null) {
postDetailsTitle.text = post.title // Error I have to still force using post!!.title
postDetailsTitle.author = post.author
Glide.with(this).load(post.featuredImage).into(postDetailsImage)
} else {
postDetailsTitle.text = "No title"
postDetailsTitle.author = "Unknown author"
Toast.makeText(this, resources.getText(R.string.post_error), Toast.LENGTH_LONG).show()
}
}
}
Run Code Online (Sandbox Code Playgroud)
我应该创建一个局部变量吗?我认为使用!!不是一个好习惯
使用该.let { }功能时,我注意到在执行以下操作时:
bucket?.assignedVariantName.let {
bucket?.determineVariant() <-- guarantee safety for bucket
}
Run Code Online (Sandbox Code Playgroud)
在这种情况下你必须保证铲斗的安全,即 bucket?.或者bucket!!在使用?.let时已经保证了零安全性,然后我注意到在执行以下操作时:
bucket?.assignedVariantName?.let { <-- added safety check for property
bucket.determineVariant() <-- doesn't need to guarantee safety for bucket
}
Run Code Online (Sandbox Code Playgroud)
虽然在桶的属性上使用let而不是直接在桶上我想知道这是故意还是Kotlin插件中的错误(在这种情况下我在Android Studio中遇到过这个)
另外的信息是,在这种情况下,存储桶是一个已local val分配的变量名是可以为空的变量.
val bucket: T? = ...
Run Code Online (Sandbox Code Playgroud) 我一直在努力开发下面提到的屏幕:
为此,我创建了以下组件:
import React, {Component} from 'react';
import {View, Text, StyleSheet, ImageBackground, Image} from 'react-native';
import Balance from './Balance.js'
class AccountHeader extends React.Component{
render(){
return(
<ImageBackground
source={require('../images/lawrance.jpg')}
style={styles.container}>
<View style={styles.overlay}></View>
<Text style = {[styles.textStyle, {paddingTop: 10}]} >My Account</Text>
<Image source= {require('../images/lawrance.jpg')}
style={styles.avatarStyle}/>
<Text style = {styles.textStyle} > Jenifer Lawrance</Text>
<Text style = {styles.textStyle} > +14155552671</Text>
<Balance style= {styles.balanceContainer}/>
</ImageBackground>
);
}
}
const styles = StyleSheet.create({
container: {
backgroundColor:'red',
opacity: 0.6
},
overlay: {
backgroundColor:'transparent',
opacity: 0.6
},
avatarStyle: { …Run Code Online (Sandbox Code Playgroud) kotlin ×6
null ×2
python ×2
android ×1
css ×1
firebase ×1
javascript ×1
nullable ×1
python-3.x ×1
react-native ×1
reactjs ×1
reflection ×1
shortcut ×1