问题
In programming language what is the difference between Inferred Type and Dynamic typing? I know about Dynamic typing but don't get how dynamic typing is differ from Inferred Type and how? Could someone please provide explanation with some example?
回答1:
Inferred type = set ONCE and at compile time. Actually the inferred part is only a time saver in that you don't have to type the Typename IF the compiler can figure it out.
Type Inference is often used in conjunction static typing (as is the case with swift) (http://en.wikipedia.org/wiki/Type_inference)
Dynamic type = no fixed Type -> type can change at runtime
static & inferred example:
var i = true; //compiler can infer that i most be of type Bool
i = "asdasdad" //invalid because compiler already inferred i is an Bool!
it is equal to
var i: bool = true; //You say i is of type Bool
i = "asdasdad" //invalid because compiler already knows i is a Bool!
==> type inference saves you spell out the type if the compiler can see it
BUT if it were dynamic that would work (e.g. objC) as the type is only based on the content at RUNtime
id i = @YES; //NSNumber
i = @"lalala"; //NSString
i = @[@1] //NSArray
回答2:
you can change data type on the fly for dynamic typing, but inferred typing does not require explicit data type declaration before use.
回答3:
Static and dynamic typing tell you when the type of the variables is checked. Static typing checks the type during compilation. Dynamic typing checks the type during runtime( on the fly)
Inferred and Manifest concerns whether you have to specify the type of the variable or not. Inferred means that the language will detect it for you. Manifest means that the type must be specified.
来源:https://stackoverflow.com/questions/24598761/inferred-type-and-dynamic-typing