如何在方法的参数中拆分案例类?
<LoginButton
readPermissions={['public_profile']}
onLoginFinished={
(error, result) => {
if (error) {
alert("login has error: " + result.error);
} else if (result.isCancelled) {
alert("login is cancelled.");
} else {
AccessToken.getCurrentAccessToken().then(
(data) => {
const infoRequest = new GraphRequest(
'/me?fields=name,picture',
null,
this._responseInfoCallback
);
// Start the graph request.
new GraphRequestManager().addRequest(infoRequest).start();
}
)
}
}
}
onLogoutFinished={() => alert("logout.")}/>
</View>
);
}
_responseInfoCallback = (error, result) => {
if (error) {
alert('Error fetching data: ' + error.toString());
} else {
alert('Result Name: ' + result.name);
}
}
}
这是获得我想要的唯一方法吗?
scala> case class f(a:Int,b:Int)
defined class f
scala> def z((a,b):f) = a + b
<console>:1: error: identifier expected but '(' found.
def z((a,b):f) = a + b
^
或者是否有更惯用的方式?
答案 0 :(得分:1)
如果您的案例类如此简单,您可以使用评论中建议的@XavierGuihot。否则,您需要模式匹配。但是,有两种方法可以使用它:您可以使用@staticmethod
def assign_attribute(xml, attribute_name, current_value):
attribute = xml.find(attribute_name)
if attribute is not None:
return attribute.text
elif current_value is not None:
return current_value
else
return None
obj.attribute1 = assign_attribute(xml_element, 'ATTRIBUTE1', obj.attribute1)
obj.attribute2 = assign_attribute(xml_element, 'ATTRIBUTE2', obj.attribute2)
关键字,也可以使用部分函数语法:
match