02Geek HTML5 and JavaScript, TypeScript, React, Flash, ActionScript online School
Previous VideoNext Video

Error 1067: Implicit Coercion

Help! I just got that dreaded 1067 error... ohh actually it's not that bad, it's telling me something: 1067: Implicit coercion of a value of type int to an unrelated type String. But what on earth does THAT mean? (We saw a headline in today's paper: "Little Girl Coerced into Car." This must be one scary bug!)

When the compiler detects something wrong with the code, it generates an error. There are different types of errors: Runtime errors and Compile time Errors. (We covered Runtime vs. Compiler errors here.) The error we're discussing here is a compile time error, so we'll learn why this error happens and how to debug it. We learned from the previous classes that the data type assigned to the variable must be same as the data type used for the declaration of variable. When the user forgets this rule, there is a pretty good probability that this error is going to show up.

For example, consider a variable that is declared initially using the data type String. Now, this variable is just like a container which can hold ONLY data of type String. So if we assign string data to this variable later in the program, it will compile normally without any errors. But if we assign a different type of data to this variable, number for example, then this will cause an error and is referred as Implicit Coercion Error.

This happens because when data is assigned to this variable, the application expects it to be a string written within double quotes such as

str="Flash";

But when a number of type integer is assigned to this variable, the compiler detects the mismatch of data types and generates an error. (02 tip: many developers have a charming term for when any error occurs: "it just puked." Hey, WE didn't invent it!) One should be very careful while assigning and declaring variables. Even after taking all possible precautions, there is still a possibility for this error to happen: after all, we're human beings. It's best to be aware of all types of errors: the syntax error (code typed wrong, function not used correctly, etc.,) the implicit error or any other type of error. When the error is a syntax error, there is no point in checking every single variable for mismatch of data type, wasting time. We can directly check for any error in syntax of the code and fix it. But when we see that the error is an implicit error, one has to realize that it is caused mostly due to a mismatch of variables and as such debug it accordingly.

Video Transcript

00:00Now what would happen if it would send a value that wasn't a string into our string now that we have it defined?
00:05so if I comment out a low world and I try to put inside of here a different value inside instead such as
00:13300 or 200 which are a number and I end it with a semicolon
00:18What would happen if I publish the file now? It's going to give me an error
00:22It's going to give me an error and it's going to tell me main in line seven. We're in line seven right now
00:28Implicit correction of a value of type int to an unrelated type string
00:35So for now, we don't really understand what type int is
00:38But it's an unrelated type because we're expecting a string
00:41So it's giving us an error the only way for us to get the data in is by sending a string
00:47Now if we wanted our number
00:50To be a string we could force it to be a string or we could wrap it
00:56inside of a
00:58quotation
01:00By wrapping it inside of a quotation and I'm going to save the file again and publishing the file
01:05You're going to see that it's not going to generate any error because because it's inside of a quotation from flash consideration
01:11It knows that we actually wanted to send a
01:14String and not a number, but if we would send a number in
01:18Flash would get confused

Ready to Level Up Your Skills?

Join thousands of learners on 02GEEK and start your journey to becoming a coding expert today!

Enroll Now for Free!