Scala - mutable (var) method parameter reference

后端 未结 7 1429
天命终不由人
天命终不由人 2020-12-08 06:17

EDIT: I keep getting upvotes here. Just for the record, I no longer think this is important. I haven\'t needed it since I posted it.

I would like to do following in

7条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-08 07:00

    Here's a couple of suggestions:

    1) Update your function a bit

    def save(srcPath: String, destPath: String) {
      var dp = destPath
      if (!dp.endsWith('/'))
        dp+= '/'
      // do something, but with dp instead of destPath
    }
    

    2) Create a utility function to use before calling save

    def savedPath(path: String) = 
      if(path.endsWith("/")) 
        path
      else
        path + "/"
    
    //call your save method on some path
    val myDestPath = ...
    val srcPath = ...
    save(srcPath, savedPath(myDestPath))
    

提交回复
热议问题