Types 此过程(elementSeparator)在ReasonML中给出了一个类型错误

Types 此过程(elementSeparator)在ReasonML中给出了一个类型错误,types,typeerror,reason,Types,Typeerror,Reason,此代码: let initialRows = 5; let initialCols = 7; /* Player 1 is P1, Player 2 is P2 */ type whichPlayer = | P1 | P2; /* Either Player 1 or Player 2 has won the game, it is a draw, or it is ongoing where e

此代码:

let initialRows = 5;
    let initialCols = 7;

    /* 
    Player 1 is P1, Player 2 is P2
    */

    type whichPlayer =
      | P1
      | P2;

    /* 
    Either Player 1 or Player 2 has won the game,
    it is a draw, or it is ongoing where either
    it is Player 1's turn or Player 2's turn
    */

    type status =
      | Win(whichPlayer)
      | Draw
      | Ongoing(whichPlayer);

    /*
    The current state of the game is represented by a list of lists of tuples: 
    containing the column number of the occupancy as the first element,
    and the occupant of the specified slot as the second element,
    and a status specification indicating which player's turn it is, or
    whether the game has ended in a win or a draw.
    */

    type state = 
      | State(list(list((int, whichPlayer))), status);

    /*
    A move is represented by an int, corresponding to
    the column number of the desired occupancy 
    */

    type move = 
      | Move(int);

let rec createEmptySpaces: int => string = num =>
    switch(num){
      | x when x > 1 => "( )" ++ createEmptySpaces(num-1)
      | 1 => "( )"
    };      

let rec elementSeparator: (list((int, whichPlayer)), int) => string = (lst, num) =>
switch(lst){
  | [(colNum, p), (colNum2, p2), ... tl] when num == 1 =>{
    let difference = colNum2 - colNum;
    if(colNum != 1){
      createEmptySpaces(colNum-1) ++
      if(p == P1){
        "(o)" ++ if(difference != 1){
                    createEmptySpaces(difference - 1)
                 }
      } else {
        "(x)" ++ if(difference != 1){
                    createEmptySpaces(difference - 1)
                 }
      } 
      ++ elementSeparator([(colNum2, p2)]@tl, num + 1)
    } else {
      if(p == P1){
        "(o)" ++ if(difference != 1){
                    createEmptySpaces(difference - 1)
                 }
      } else {
        "(x)" ++ if(difference != 1){
                    createEmptySpaces(difference - 1)
                 }
      }
      ++ elementSeparator([(colNum2, p2)]@tl, num + 1)
    }
  }

  | [] => ""
};

elementSeparator([(1, P1), (3, P2), (7, P1)], 1);
elementSeparator([(3, P2), (5, P2)], 1);
elementSeparator([(2, P1), (3, P2), (4, P1), (6, P2)], 1);
给了我一个错误:

Error: This expression has type string but an expression was expected of type unit
对于第三个if表达式。我正在尝试创建一行
“(x)”、“(o)”及“()”等。我不知道为什么它会抛出这个错误,因为elementSeparator应该输出一个字符串。每次我尝试在第二个if表达式之后连接另一个字符串(通过函数、过程、表达式等),它都会抛出此错误。有人能帮我吗?

如果表达式没有
其他
分支,则总是需要返回
单位
类型的值(因为如果
如果
if
条件未解析为
,则这是表达式的类型)

可以通过添加一个返回空字符串的
else
分支来解决此问题,或者为了简洁起见,只使用三元运算符:

difference != 1 ? createEmptySpaces(difference - 1) : ""

请不要破坏你的帖子,为别人做更多的工作。通过在Stack Exchange(SE)网络上发布,您已经在a下授予SE分发内容的不可撤销权利(即,无论您未来的选择如何)。根据SE政策,分发非故意破坏版本。因此,任何故意破坏行为都将恢复原状。请参阅:。如果允许删除,则在帖子下方左侧有一个“删除”按钮,但它仅在浏览器中,而不是移动应用程序中。