Why no i++ in Scala?

前端 未结 11 1826
半阙折子戏
半阙折子戏 2020-12-23 10:48

I just wonder why there is no i++ to increase a number. As what I know, languages like Ruby or Python doesn\'t support it because they are dynamically typed. So

相关标签:
11条回答
  • 2020-12-23 11:29

    The question to ask is why there should be such an operator, not why there shouldn't be. Would Scala be improved by it?

    The ++ operator is single-purpose, and having an operator that can change the value of a variable can cause problems. It's easy to write confusing expressions, and even if the language defines what i = i + i++ means, for example, that's a lot of detailed rules to remember.

    Your reasoning on Python and Ruby is wrong, by the way. In Perl, you can write $i++ or ++$i just fine. If $i turns out to be something that can't be incremented, you get a run-time error. It isn't in Python or Ruby because the language designers didn't think it was a good idea, not because they're dynamically typed like Perl.

    0 讨论(0)
  • 2020-12-23 11:32

    As another answer suggests, the increment operator, as found in i++, was—

    supposedly ... added to the B language [a predecessor of the C language] by Ken Thompson specifically because [it was] capable of translating directly to a single opcode once compiled

    and not necessarily because such an operator is as useful to have as, say, general addition and subtraction. Although certain object-oriented languages (such as Java and C#) also have an increment operator (often borrowed from C), not all do (such as Ruby).

    0 讨论(0)
  • 2020-12-23 11:33

    Scala doesn't have i++ because it's a functional language, and in functional languages, operations with side effects are avoided (in a purely functional language, no side effects are permitted at all). The side effect of i++ is that i is now 1 larger than it was before. Instead, you should try to use immutable objects (e.g. val not var).

    Also, Scala doesn't really need i++ because of the control flow constructs it provides. In Java and others, you need i++ often to construct while and for loops that iterate over arrays. However, in Scala, you can just say what you mean: for(x <- someArray) or someArray.foreach or something along those lines. i++ is useful in imperative programming, but when you get to a higher level, it's rarely necessary (in Python, I've never found myself needing it once).

    You're spot on that ++ could be in Scala, but it's not because it's not necessary and would just clog up the syntax. If you really need it, say i += 1, but because Scala calls for programming with immutables and rich control flow more often, you should rarely need to. You certainly could define it yourself, as operators are indeed just methods in Scala.

    0 讨论(0)
  • 2020-12-23 11:36

    Of course you can have that in Scala, if you really want:

    import scalaz._, Scalaz._
    
    case class IncLens[S,N](lens: Lens[S,N], num: Numeric[N]) { 
      def ++ = lens.mods(num.plus(_, num.one))
    }
    
    implicit def incLens[S,N: Numeric](lens: Lens[S,N]) =
      IncLens[S,N](lens, implicitly[Numeric[N]])
    
    val i = Lens.lensu[Int,Int]((x, y) => y, identity)
    
    val imperativeProgram = for {
      _ <- i++;
      _ <- i++;
      x <- i++
    } yield x
    
    def runProgram = imperativeProgram exec 0
    

    And here you go:

    scala> runProgram
    res26: scalaz.Id.Id[Int] = 3
    

    No need to resort to violence against variables.

    0 讨论(0)
  • 2020-12-23 11:38

    Scala encourages using of FP style, which i++ certainly is not.

    0 讨论(0)
  • 2020-12-23 11:40

    You could simulate it, though. As a trivial example:

    scala> case class IncInt(var self: Int = 0) { def ++ { self += 1 } }
    defined class IncInt
    
    scala> val i = IncInt()
    i: IncInt = IncInt(0)
    
    scala> i++
    
    scala> i++
    
    scala> i
    res28: IncInt = IncInt(2)
    

    Add some implicit conversions and you're good to go. However, this sort of changes the question into: why isn't there a mutable RichInt with this functionality?

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