What are the benefits (and drawbacks) of a weakly typed language?

后端 未结 7 884
情书的邮戳
情书的邮戳 2020-12-11 00:52

I\'m a big fan of PHP and it\'s obviously a very weakly-typed language. I realize some of the benefits include the general independence of changing variable types on the fly

7条回答
  •  一生所求
    2020-12-11 01:31

    Many a book has been written about this sort of thing. There's an inherent tradeoff; with a weakly-typed language a lot of annoyances simply cease to be. For instance, in Python you never have to worry about dividing a float by an int; adding an int to a list; typing functions' arguments (did you know, OCaml has special +. operators for adding floats because (+) sends ints to ints!); forgetting that a variable can be null... those sorts of problems simply vanish.

    In their place come a whole host of new runtime bugs: Python's [0]*5 gives, wait for it, [0,0,0,0,0]! OCaml, for all the annoyance of strong typing, catches many many bugs with its compiler; and this is precisely why it's good. It's a tradeoff.

提交回复
热议问题