I wondered if there was a generalized name for typing a variable by assigning a specific value to it. For instance

  a = 4

This would make the variable a an integer, since 4 is an integer. Likewise

  b = 0.8

is b a float in this instance.

Technically this is not dynamic typing (since there is only one defined type, it is just written as the value), but then how is this form called?

  • 1
    $\begingroup$ en.wikipedia.org/wiki/Type_inference $\endgroup$ – JimN Dec 14 '17 at 23:17
  • 1
    $\begingroup$ Note that things may not always be a clear-cut as you think. Why int and not long, short, or double here? Implementations and answers differ between languages. $\endgroup$ – Raphael Dec 15 '17 at 11:10

Determining the type of a variable from the type of the value that is assigned to it is a form of type inference.

In dynamically typed languages, variables usually don't have types, only values have types. In statically typed languages, variables do have types. Most modern statically typed general purpose languages have a form of type inference that can at least infer the type of a variable from the type of what is assigned to it.

Type inference can infer types in other circumstances. The power of type inference depends on the language: full type inference limits the expressive power of the language, as there are many useful typed languages where full type inference is undecidable, but type verification is easy. Different languages make different compromises on automatic inference vs expressiveness.

  • $\begingroup$ So if I understood correctly, type inference is used, but not limited to dynamic languages, right? $\endgroup$ – Murad Babayev Dec 15 '17 at 8:05
  • 1
    $\begingroup$ @MuradBabayev Type inference is a feature of some statically typed languages. In most dynamically typed languages, it isn't even meaningful since the language doesn't have a concept of relating a type to a variable. $\endgroup$ – Gilles Dec 15 '17 at 10:51
  • $\begingroup$ @MuradBabayev Type inference, if powerful enough, makes statically types code look similar to code in "dynamic" languages. That doesn't mean "dynamic" languages use type inference. They don't, as Gilles explains. $\endgroup$ – Raphael Dec 15 '17 at 11:12
  • $\begingroup$ That makes sense. Approving. $\endgroup$ – Murad Babayev Dec 15 '17 at 21:20
  • $\begingroup$ @Gilles But then, what is the term "implicit typing", and how does it differ? $\endgroup$ – Murad Babayev Dec 16 '17 at 9:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.