When reading about data types, "type definition" is often used without any prior explanation to what it means.

For example, in https://en.wikipedia.org/wiki/Type_system, we have the sentence "... since C++ has stronger rules regarding type definitions ..."

Any help in clarifying what the difference between, for example, type and type definition is greatly appreciated!

  • 3
    $\begingroup$ Do you know what a type is? Then, a type definition is, as the term suggests, the definition of a type. I'm not sure what your real question is here. $\endgroup$ – Raphael May 5 '17 at 5:15

"Defining" a type is the act of building a new type composed of one or more pre-existing data types. For example, the "Complex Number" type is often declared as an ordered pair of "Real Numbers" :

Type Complex
  Real a
  Real b

a and b being the real and imaginary parts.

This sample of pseudo-code is a "Type Definition".

| cite | improve this answer | |
  • $\begingroup$ I don't think this is what's being asked... Defining an abstract data type is of course a type definition, but has nothing to do with strong or weak typing (part of the question) $\endgroup$ – EL Dendo May 5 '17 at 10:08

From "The C++ Programming Language" -by Bjarne Stroustrup the creator of C++.

A type defines a set of possible values and a set of operations (for an object).

Also the following definitions are interrelated with type taken from the same book

A declaration is a statement that introduces a name into the program.It specifies a type for the named entity

An object is some memory that holds a value of some type.

A value is a set of bits interpreted according to a type.

A variable is a named object

| cite | improve this answer | |
  • $\begingroup$ This doesn't mention type definition. $\endgroup$ – reinierpost Jan 7 at 14:41

Variables can contain different sorts of values, e.g., integers, reals, characters... This is called the TYPE of the variable. Some languages are dynamically typed. That means that the interpreter or the compiler decides the type of the variable for you. In static typed languages, you have to define the type yourself when declaring the variable. This is the TYPE DEFINITION.

In a strong typed static languages the rules forbid to mix different types. if you want to add, e.g., a real and an integer you have to explicitly cast the integer to real.

in a weak typed language you can mix types. but since the low level bitpatterns differs between types, you can have unwanted results.

Most languages are somewhere in between. It will be clear that unwanted effects are not acceptable, while the need for an explicit cast makes the code heavy. So most compilers will do an implicit cast for some standard operations. Adding a real and an integer will in that case implicitly provoke a cast to real.

| cite | improve this answer | |
  • $\begingroup$ What is "p.e."? Also, I don't see how what you've written after the first paragraph relates to the question. $\endgroup$ – David Richerby May 5 '17 at 11:11
  • $\begingroup$ @ David: p.e. = exempli gratia = For the sake of example = for instance... The sentence "... since C++ has stronger rules regarding type definitions ..." shows in my opinion that the question is asked in the context of weak and strong typing. $\endgroup$ – EL Dendo May 5 '17 at 12:11
  • $\begingroup$ Aha -- that's "e.g.", in English -- edited. $\endgroup$ – David Richerby May 5 '17 at 12:17
  • $\begingroup$ indeed it is. my mistake. $\endgroup$ – EL Dendo May 5 '17 at 12:19
  • 2
    $\begingroup$ This answer demonstrates a variety of serious misunderstandings of types and type systems. Virtually every sentence of this answer is incorrect, confusing, or misleading. As a start, type systems are not just about variables: 3+'a' doesn't involve any variables but will be type checked. Dynamically "typed" language implementations don't "decide" the type of anything. Implicit conversions don't make a language more "dynamically" or "weakly" typed. Generally, static and dynamic "typing" are not opposite ends of a spectrum but are simply talking about different things entirely. $\endgroup$ – Derek Elkins left SE May 5 '17 at 22:18

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.