如何在 Scala 的 else 块中返回空内容?

huangapple go评论88阅读模式
英文:

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.

huangapple
  • 本文由 发表于 2020年9月23日 22:00:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/64029667.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定