You are not logged in.
If you're after type safety, why stop at Python? Perhaps Haskell would be more suited?
I started learning Haskell a while ago and it's definitely a fantastic language, but I don't think it would be good for "the masses", since it's not the easiest language to get started with quickly. I might decide to use it to make a couple web apps in the future though.
In Zen they say: If something is boring after two minutes, try it for four. If still boring, try it for eight, sixteen, thirty-two, and so on. Eventually one discovers that it's not boring at all but very interesting.
~ John Cage
Offline
function privileges($username) { if ($username === "TheAdmin") { return "Admin"; } elseif ($username === "SomeUser") { return "User"; } return False; } echo (privileges(True));
I don't even understand what that function is supposed to accomplish. Something like this would easily suffice:
function privileges($username) {
// Check if user is admin
if ($username == "admin") {
return true;
}
else {
return false;
}
}
$username = "notadmin";
if (privileges($username) == true) {
echo "Welcome boss";
}
else {
echo "GTFO";
}
Offline
I don't even understand what that function is supposed to accomplish. Something like this would easily suffice:
The point of the example was to demonstrate type safety. If the programmer is always sufficiently clever, then type safety is moot.
Moreover, your example is not isomorphic to the original. The original example returns what sort of privileges $username has; your example returns whether the user has administrative access or not.
Education is favorable to liberty. Freedom can exist only in a society of knowledge. Without learning, men are incapable of knowing their rights, and where learning is confined to a few people, liberty can be neither equal nor universal.
Tu ne cede malis sed contra audentior ito
Offline
The point of the example was to demonstrate type safety. If the programmer is always sufficiently clever, then type safety is moot.
Moreover, your example is not isomorphic to the original. The original example returns what sort of privileges $username has; your example returns whether the user has administrative access or not.
No, I mean that the function a) serves no apparent purpose, and b) does in fact output false, when he clearly stated that it wouldn't.
function privileges($username)
{
if ($username === "TheAdmin")
{
return "Admin";
}
elseif ($username === "SomeUser")
{
return "User";
}
return False;
}
if (privileges(True) == true) {
echo "true";
}
else {
echo "false";
}
That outputs "false." Regardless, this function seems absolutely useless to me.
My intent was not to replicate his function, but here, if you would rather have something similar:
function privileges($username) {
// Check if user is admin
if ($username == "admin") {
return "admin";
}
else {
return "user";
}
}
$username = "notadmin";
if (privileges($username) == "admin") {
echo "Welcome boss";
}
else {
echo "GTFO";
}
The original function supports his argument against weak typing in no way; hell, it doesn't even serve its own purpose. It's like ordering a pizza and then having an elephant fall through your roof.
Last edited by cesura (2011-08-24 02:10:18)
Offline
The point of the example isn't that relevant, but basically: say you have a function that takes a username, checks it against a string, and if they match outputs the privileges that user is supposed to have. The point was to show that using == to test whether the the two are the same can cause unexpected bugs if the type that gets used with the function isn't a string.
Obviously you would never use a function like this in a real program, you would be using some kind of database to store user information.
In the second function that you posted, the same problem exists. If $username isn't a string, then the program will react in an unexpected way (to a new programmer, or someone without much experience using weakly typed languages).
How would $username get that way? It's very possible that you could have a bug in another function that gives you a username that causes it to output a Boolean instead of a String. You would never know about the existence of either of these bugs if you had a weakly typed language.
In Zen they say: If something is boring after two minutes, try it for four. If still boring, try it for eight, sixteen, thirty-two, and so on. Eventually one discovers that it's not boring at all but very interesting.
~ John Cage
Offline
The point of the example isn't that relevant, but basically: say you have a function that takes a username, checks it against a string, and if they match outputs the privileges that user is supposed to have. The point was to show that using == to test whether the the two are the same can cause unexpected bugs if the type that gets used with the function isn't a string.
Obviously you would never use a function like this in a real program, you would be using some kind of database to store user information.
In the second function that you posted, the same problem exists. If $username isn't a string, then the program will react in an unexpected way (to a new programmer, or someone without much experience using weakly typed languages).
How would $username get that way? It's very possible that you could have a bug in another function that gives you a username that causes it to output a Boolean instead of a String. You would never know about the existence of either of these bugs if you had a weakly typed language.
Well, that's obviously a very specific and very unlikely case. Registration forms transfer text box data as strings ($_POST['username'] or something). A database will store it with some sort of string association. When it's retrieved by the program, it will be a string.
You would almost have to consciously make an effort to screw this up. Saying "look at how I'm able to get this language to fail" isn't a strong method of denouncing it. "You can easily overflow buffers in C. C IS TEH SUX0RZ!"
I was basically asking how your example demonstrates a weakness of weak typing. I understand what you're trying to explain, but you don't seem to be providing anything that exemplifies it, especially considering that it outputs "false" instead of "true."
Last edited by cesura (2011-08-24 02:42:11)
Offline
Examples aren't needed to support Nisstyre56's case. A more strongly typed language is guaranteed to catch a greater class of type-related bugs than a more weakly typed language. See Type safety on Wikipedia. My original point was only whether that trade-off is significant enough to be worth it in the case of PHP vs. Python.
Education is favorable to liberty. Freedom can exist only in a society of knowledge. Without learning, men are incapable of knowing their rights, and where learning is confined to a few people, liberty can be neither equal nor universal.
Tu ne cede malis sed contra audentior ito
Offline