Scala - mutable (var) method parameter reference

后端 未结 7 1426
天命终不由人
天命终不由人 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:21

    No, that's not allowed in Scala. Others have described some low-level workarounds (all good), but I'll add a higher-level one. For just this sort of string normalization purposes, I keep around a pimped extension to scala.String with methods like suffix, prefix, removeSuffix, and removePrefix. suffix and prefix append or prepend one string onto another, unless the suffix or prefix is already there. removeSuffix and removePrefix do the obvious, removing one string from the end or beginning of another, if it's present. Your use case would be written

    val normalizedPath = destPath.addSuffix("/") 
    

    If you do a bunch of data analysis or file operations, these methods are so handy that you won't believe that you ever did without them.

    0 讨论(0)
提交回复
热议问题