Sumit Sagar
Sumit Sagar

Reputation: 127

Type Checking in Compiler

Is it possible to type check an expression , say x+y , in a language where variables are not declared before use ? Can someone please explain with an example. Thanks in advance .

Upvotes: 1

Views: 595

Answers (1)

sepp2k
sepp2k

Reputation: 370455

The main effect of not having variable declarations with regards to type checking would be that your variables don't have explicitly declared types. So your question can be reduced to whether it is possible to type check expressions in languages with no or optional type signatures. The answer to that is yes since there are statically typed languages where type signatures are optional and the types are simply inferred.

An example of this would be Haskell:

f x y = x + y

Here f gets the type Num a => a -> a -> a, which means that it takes two numbers of the same type and returns another number of that type. This type is automatically inferred by the compiler if you don't provide it explicitly.

Upvotes: 2

Related Questions