Skip to main content
gamedev.net
🔒 Locked

Type Checking

Started by Barn Door 5 replies 250+ views
Original Post
Barn Door
Barn Door
Hi there, Please can someone help me to understand the value of type checking in a computer language? As far as I can tell, it is to protect the integrity of memory such that one cannot accidentally access memory which one is not explicitly intending too. Thanks, BD.
Martee
Martee
Suppose that type checking did not exist. What should the result of executing the following code be?

string s = "Some Useless String";
int i = 12345;
float f = s * i;

[edited by - Martee on March 21, 2003 12:38:09 PM]
ReactOS - an Open-source operating system compatible with Windows NT apps and drivers
Barn Door
Barn Door
Hi,

Thanks for your reply.

I think that each character in the string would be converted to its numerical code and then the string would then be a long number. Then it would be a simple multiplication which would be writen into memory starting at the address of the float.

BD.
LilBudyWizer
LilBudyWizer
If I hand you a wrench and then say give me that screwdriver back you would say what screwdriver, you gave me a wrench. Maybe I meant to say wrench and the wrench is what I want. Maybe I really meant screwdriver and I only thought I handed it to you so now I have to figure out what I did with it. Same basic idea. Rather than just blindly doing whatever you ask it checks that you are at least consistant in what you call things.
Keys to success: Ability, ambition and opportunity.
davepermen
davepermen
quote:
Original post by LilBudyWizer
If I hand you a wrench and then say give me that screwdriver back you would say what screwdriver, you gave me a wrench. Maybe I meant to say wrench and the wrench is what I want. Maybe I really meant screwdriver and I only thought I handed it to you so now I have to figure out what I did with it. Same basic idea. Rather than just blindly doing whatever you ask it checks that you are at least consistant in what you call things.


my brain hurts.....

"take a look around" - limp bizkit
www.google.com
If that's not the help you're after then you're going to have to explain the problem better than what you have. - joanusdmentia
My Page davepermen.net | My Music on Bandcamp and on Soundcloud
Fruny
Fruny
Be aware, though, that a number of languages (SmallTalk, Perl, Python, Lisp, ...) use dynamic typing, where the type is not associated with the variable, but with the data itself.

Martee''s code would become something like

s = "Some Useless String";
i = 12345;
f = float(s * i);

And the computer would complain at run time that it can''t multiply a string by an int, or (if it can) that it cannot make a floating point number out of the result.

This adds a lot of flexibility to the language - whether you have a string, list or a number, if a+b is a valid operation, you can use it. The computer will catch the cases where you can''t, without you having to worry about specifying the type ''just so'' (often through a cosmic class hierarchy).

Of course, the downside is that many errors are not caught until run-time, while static typing would have caught them at compile time. In C++, templates let you get some of that flexibility back, by generating any type-based variant of a common code template with little intervention of the user (once you''ve beaten the template system into working the way you want it).

Programming being all about trade-offs, you''ve got to decide what approach works for you.


[ Start Here ! | How To Ask Smart Questions | Recommended C++ Books | C++ FAQ Lite | Function Ptrs | CppTips Archive ]
[ Header Files | File Format Docs | LNK2001 | C++ STL Doc | STLPort | Free C++ IDE | Boost C++ Lib | MSVC6 Lib Fixes ]
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
the Speed Bump
the Speed Bump
This is offtopic, but Python allows multiplying strings by ints.

a = ''ni'' * 3

a is now ''ninini''

I''m hip because I say "M$" instead of "MS".
"There is only one everything"

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.