top | item 5275405

(no title)

Spykk | 13 years ago

I think dynamically typed languages have become so popular because in simple examples they look great to novices. You don't need to know much syntax about defining variable types, you don't have to type as much, you don't have to figure out why adding an integer to the end of your string won't work, etc. Later, when they try to build something large enough to be useful they realize just how much they traded for that little bit of convenience. For example in PHP:

  $id = 1;

  $dbh = new PDO($connectionString, $user, $password);

  $sql  = ' SELECT column';
  $sql .= ' FROM table';
  $sql .= ' WHERE id = :id';

  $stmt = $dbh->prepare($sql);
  $stmt->bindParam(':id', $id);

  if($stmt->execute()) {
    // Do stuff
  }

  //This echos ID: 1
  echo "ID: $id";

  if($id === 1) {
    echo 'This should be true, we set $id = 1 at the top.';
  } else {
    echo 'Instead we get here because PDO casually changed our variable into a string and "1" != 1.';
    die('in a fire dynamic typing.');
  }

discuss

order

ScottBurson|13 years ago

> you don't have to figure out why adding an integer to the end of your string won't work

You're confusing dynamic typing with weak typing. A language can be dynamically typed (types are checked at runtime) without doing things like automatically converting integers to strings, integers and strings to booleans, etc. etc.

Conversely, a language can be statically typed and still do some of those conversions.