英文:
How to return nothing from an else block of an IF Condition in scala?
问题
我正在将一个Java项目迁移到Scala,并遇到了下面的代码:
private def searchClauses(select: TCustomSqlStatement): Unit = {
if (!searchInClauses.contains(select)) {
searchInClauses.add(select)
} else {
return
}
if (select.isInstanceOf[TSelectSqlStatement]) {
val statement = select.asInstanceOf[TSelectSqlStatement]
val clauseTable = new HashMap[TExpression, ClauseType]
if (statement.getWhereClause != null) {
clauseTable.put(statement.getWhereClause.getCondition, ClauseType.where)
}
for (expr <- clauseTable.keySet.toArray(new Array[TExpression](0))) {
val `type` = clauseTable.get(expr)
searchExpression(expr, select, if (`type` == null) null else `type`.name)
searchTables(select)
}
}
}
在IF条件的else块中,前任开发人员只写了return
,甚至没有返回任何内容。如果它不返回null
,那么只写return
有什么用呢?
由于我无法理解那行代码,我不知道如何将那个特定的IF条件转换为Scala。
是否有人可以告诉我如何在Scala中编写相同的代码块,以确保不影响第一个IF-ELSE条件之下的代码?
英文:
I am working on migrating a Java project to Scala and encountered the below code:
private void searchClauses( TCustomSqlStatement select ) {
if ( !searchInClauses.contains( select ) ) {
searchInClauses.add( select );
}
else {
return;
}
if ( select instanceof TSelectSqlStatement ) {
TSelectSqlStatement statement = (TSelectSqlStatement) select;
HashMap clauseTable = new HashMap( );
if ( statement.getWhereClause( ) != null ) {
clauseTable.put( ( statement.getWhereClause( ).getCondition( ) ),
ClauseType.where );
}
for ( TExpression expr : (TExpression[]) clauseTable.keySet( ).toArray( new TExpression[0] ) ) {
ClauseType type = (ClauseType) clauseTable.get( expr );
searchExpression( expr,
select,
type == null ? null : type.name( ) );
searchTables( select );
}
}
}
In the else block of the IF condition, the previous developer wrote just return
and it is not even returning anything. If it is not returning null
, what is the use of having just return
?
Since I couldn't understand that particular line, I don't understand how to convert that particular IF condition in Scala.
Could anyone let me know how can I write the same block in Scala in a way that it doesn't impact the code below the first IF-ELSE condition ?
答案1
得分: 4
在这种情况下的简单解决方案是将第二个if
放在第一个if
内部,并删除else
部分。
英文:
The simple solution in this case is to put the second if
inside the first if
and remove the else
part.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论