2
$\begingroup$

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!

$\endgroup$
1
  • 4
    $\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
    Commented May 5, 2017 at 5:15

5 Answers 5

3
$\begingroup$

"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
End

a and b being the real and imaginary parts.

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

$\endgroup$
1
  • 1
    $\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
    Commented May 5, 2017 at 10:08
3
$\begingroup$

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

$\endgroup$
1
  • 1
    $\begingroup$ This doesn't mention type definition. $\endgroup$ Commented Jan 7, 2020 at 14:41
0
$\begingroup$

Type Systems

The page you link to is about type systems. The differences most people should be aware of when starting out is static vs dynamic typing and strong vs weak typing (though feel free to explore and ask about more).

Type systems are logical rules built into compilers and interpreters for assigning types (indicative of the kind of data something holds) to each term (variables, expressions, functions, modules) in a program, i.e. identify something as 'a list of ints' or 'an array of bools' or 'an array of arrays of chars'.

Strong vs Weak Typing

Strong vs weak typing are relatively easy to understand, though these are sometimes used a bit loosely. I think these terms are best used to describe individual features than languages as a whole, though you can certainly argue, for instance, that some languages are more strongly typed than others.

Strong typing features include stricter type-checking at compile time or run time, checking the types at assignments, returns, function calls and so on to look for possible violations, and minimal implicit type conversions (except possibly those that lose no data, e.g. an int to a float). Weak typing features include the opposite. The extreme example is assembly, which is technically typeless - the concept of types simply doesn't exist. Another example is liberal type conversions, including those that may lose some information. A rather interesting example is pointer arithmetic in a language like C, which makes it easy to get around the type system. C++, for instance, requires each declaration to explicitly carry a type, so you can only have something like int x or double y or bool* f. In the line you quote, the page explicitly compares templatised code (roughly, generic code that can be reused with different types). In C++, that requires explicit type specification when a templatised function is called to guide the compiler.

// templatised code
template <typename T>
T add(T a, T b) {
  return a + b;
}

// assuming everything is declared, call as
sumD = add<double>(3.1415, 1.4489);
/*
// The compiler generates and adds
double add(double a, double b) {
  return a + b;
}
*/

sumI = add<int>(7, 2);
/*
// The compiler generates and adds
int add(int a, int b) {
  return a + b;
}
*/

Python, by comparison, uses 'duck typing' - 'if it walks like a duck and quacks like a duck, it must be a duck'. In Python terms, an object may be used in any context that it supports. This makes template code much easier to write, since you don't have to specify the type you're passing at every call.

Static vs Dynamic Type Checking

Type checking can be done either statically (at compile time) and dynamically (at run time). Static and dynamic type checking is one example of static and dynamic analysis, the former using the source code, and the latter using the state at runtime. Unlike strong and weak typing, these two are not 'somewhat subjective'.

There are tradeoffs to which a language implements. Static type checking, for instance, allows the compiler to make extra optimisations and allows predicting some errors based on the source code alone, though at the cost of more boilerplate and sometimes complex error messages. Dynamic typing allows more polymorphism, at the cost of possibly more runtime errors and therefore more testing and maintenance, besides prohibiting some optimisations that static typing would allow.

Most languages that use static type checking combine it with some form of dynamic type checking as well to catch runtime errors or cover features such as downcasting (casting a base class pointer to one of its derived classes), which are impossible to statically type check.

$\endgroup$
0
$\begingroup$

A type definition is the creation of a new type, that gets a name. Can be a typedef, a class/struct/union/enum definition...

Simple example:

typedef int MyInt;

creates the type MyInt.

$\endgroup$
-1
$\begingroup$

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.

$\endgroup$
5
  • $\begingroup$ What is "p.e."? Also, I don't see how what you've written after the first paragraph relates to the question. $\endgroup$ Commented May 5, 2017 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
    Commented May 5, 2017 at 12:11
  • $\begingroup$ Aha -- that's "e.g.", in English -- edited. $\endgroup$ Commented May 5, 2017 at 12:17
  • $\begingroup$ indeed it is. my mistake. $\endgroup$
    – EL Dendo
    Commented May 5, 2017 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$ Commented May 5, 2017 at 22:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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