Scala: Can a literal reference itself?

前端 未结 2 573
名媛妹妹
名媛妹妹 2020-12-20 23:08

I want to do something like this:

scala> \"Hello world\"(this.length -1)
res30: Char = d

This obviously doesn\'t work as I can\'t refere

2条回答
  •  自闭症患者
    2020-12-20 23:41

    You can't reference the literal itself, but you can create a block with a temporary variable local to that block.

    scala> val lastChar = { val tmp = "Hello World"; tmp(tmp.length - 1) }
    lastChar: Char = d
    
    scala> tmp
    :8: error: not found: value tmp
    

提交回复
热议问题