Tutorial :What is a strictly typed language? [duplicate]



Question:

What is a strictly typed language?


Solution:1

Strictly typed languages enforce typing on all data being interacted with.

For example

int i = 3  string s = "4"  

From here on out, whenever you use i, you can only interact with it as an integer type. That means you are restricted to using with methods that work with integers.

As for string s you can only interact with it as a string type. You can concatenate it with other string, print it out, etc. However, even though it contains that character "4", you cannot add to an integer without using some function to convert the string to an integer type.

In a dynamically typed language, you have a lot more flexibility:

i = 3  s = "4"  

Types are inferred; meaning they are determined based on the data they are set to. i is obstensively a number type, and s is a string type, based on how they were set. However when you have i + s; type inference is used and depending on your environment, you may get the result i + s = 7; since s was implicitly converted to an int by the programming environment. However, this operation could also result in the string "34", if the environment infers an int + string should equal a concatenation operation vs an addition operation.

This flexibility has made loosely typed languages very popular. However, because these type inference can sometimes produce unexpected results; they can also result in more bugs in your code if you're not careful. In a typed language, if I perform i + s, I am forced by the compiler to change s into an int first, so I know by adding i to s, I will get 7 because I was forced to convert s to an explicit int first. In a dynamic language, it attempts to do this for you implicitly, but the results may not be what you were expecting, since anything can be in i or s; a string, a number, or even an object. You don't know until you run your code and see what happens.


Solution:2

Languages where variables must be declared to contain a specific type of data.

If your variable declarations look like:

String myString = "Fred";  

then your language is strictly typed, variable "myString" is explicitly declared to contain only string data.

If the following works:

x = 10;  x = "Fred";  

then it's loosely typed (two different types of data in the same variable and scope).


Solution:3

There's dissenting opinions about how strong or weak various type systems are, but I've generally heard "strictly typed programming language" to mean a very strongly typed programming language. This often describes the static type systems found in several functional languages.


Solution:4

I tried to look up "strict typing" and wasn't able to find a definitive definition for the term. Perhaps it refers to a strongly typed language?

Strong typing refers to a type system in which there are restrictions to the operation on which two variables of different types can be performed. For example, in a very strongly typed language, trying to add a string and number may lead to an error.

string s;  number n;  s + n;          <-- Type error.  

The error may occur at compile time for statically typed languages or at runtime for dynamically typed languages. It should be noted that static/dynamic and strong/weak may sound like similar concepts, they are quite different.

A less strongly typed language may allow casting of variables to allow operations between variables originating from different types:

s + (string)n;  <-- Allowed, as (number) has been explicitly                      casted to (string), so variable types match.  

In a weakly typed language, variables of differing types may become automatically casted to compatible types.

s + n;          <-- Allowed, where the language will cast                      the (number) to (string)  

Perhaps, the "strictly typed language" refers to a very strongly typed language in which there are more strict restrictions as to how operations can be performed on variables of different types.


Solution:5

languages where '1' + 3 would be illegal, because it's adding a string to an integer.


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »