in reply to Re: Static typing is mostly a waste of time
in thread Static typing is mostly a waste of time

sqrt("four") eq "two"

Seriously, it's a question of being willing to pay the price of validating the input yourself. In many cases you have to do it anyway; for example, for square roots even if you can trust the compiler that you are getting a number, you have to make sure that it is not negative (unless your sqrt function deals with complex numbers).

Replies are listed 'Best First'.
Re^3: Static typing is mostly a waste of time
by hardburn (Abbot) on Apr 12, 2005 at 14:53 UTC

    Type systems don't concern themselves with what data you can put into the variable. They concern themselves with what you're allowed to do with that variable. Mathmatically, it makes no sense to take the square root of a string, or call an insert() method on an object that only runs selects, or give a background the color of laptop.

    The holy grail is a program that is proven correct just by the fact that you compiled it without errors. Good, strong type systems are a major step in that direction. I'm not sure we'll ever actually reach that point, but it's good to try.

    "There is no shame in being self-taught, only in not trying to learn in the first place." -- Atrus, Myst: The Book of D'ni.

Re^3: Static typing is mostly a waste of time
by Anonymous Monk on Apr 12, 2005 at 15:18 UTC
    Or unless you have a type "Non-negative number".
      Or unless you have a type "Non-negative number".

      Which you can define easily in Perl 6 :):

      my subtype NonNegativeNumber of Num where { not $^a < 0 }; my NonNegativeNumber $x = 3; # works my NonNegativeNumber $y = -3; # dies

      Another example of using subtypes:

      my subtype HTTP::Request::Method of Str where { $^method eq any <GET POST HEAD ...>; }; method send_using(HTTP::Request::Method $method) { ...; # Note: You don't have to manually check for $method # not being a valid HTTP method :). }

      --Ingo

Re^3: Static typing is mostly a waste of time
by Anonymous Monk on Apr 12, 2005 at 15:50 UTC
    ...for square roots even if you can trust the compiler that you are getting a number, you have to make sure that it is not negative (unless your sqrt function deals with complex numbers).
    Even if your sqrt doesn't do complex numbers, you'll still get an exception for negative answers. Which seems like a better alternative than returning a garbage answer.

    $a = ["abc",123]; $b = -1; print sqrt($a); print sqrt($b);
Re^3: Static typing is mostly a waste of time
by dragonchild (Archbishop) on Apr 12, 2005 at 15:08 UTC
    Actually, sqrt("four") eq "0". Try it if you don't believe me! :-)