4

The terms "declaration" and "definition" are being used synonymously in Apple's Swift documentation and it's getting me confused.

Under the "Initialization" section (which talks about class initializers), Apple states:

You can set an initial value for a stored property within an initializer, or by assigning a default property value as part of the property’s definition.

Further in a subsection they state:

You can set the initial value of a stored property from within an initializer, as shown above. Alternatively, specify a default property value as part of the property’s declaration.

I thought a variable declaration was different than a variable definition.

Walter M
  • 4,993
  • 5
  • 22
  • 29

4 Answers4

2

You are right that those two mean different thing, THOUGH I think most of the people just use both of them in the same meaning and I think that is also the case of those AppleDocs. Here is great article on subject:

Summary

A declaration provides basic attributes of a symbol: its type and its name. A definition provides all of the details of that symbol--if it's a function, what it does; if it's a class, what fields and methods it has; if it's a variable, where that variable is stored. Often, the compiler only needs to have a declaration for something in order to compile a file into an object file, expecting that the linker can find the definition from another file. If no source file ever defines a symbol, but it is declared, you will get errors at link time complaining about undefined symbols.

Jiri Trecak
  • 5,092
  • 26
  • 37
  • 1
    Would be nice to have it confirmed from some native english speaking person, which I am not ; maybe I am missing some nuances in the docs that could differentiate it. – Jiri Trecak Jul 23 '15 at 07:29
2

After doing much searching across the web for legitimate explanations, I have seemed to have found an answer:

The problem is that the two terms overlap to some extent. Definitions also serve as declarations, because they inject an identifier of a certain type to a scope. However, a declaration isn't a definition because it doesn't entail storage allocation for the declared object. To add to the confusion, the semantics of definitions and declarations is slightly different when applied to types and functions, as I will show momentarily. So let's look at a more detailed analysis of these two terms.

Here is the article: Declarations and Definitions.

The article gives further explanation and examples.

Walter M
  • 4,993
  • 5
  • 22
  • 29
1

Declaration of variable mean to tell compiler their is a var\funct\struct of particular data type. Definition of variable mean asking compiler to allocate memory to variable or define storage for that variable. you can define a variable only one time but you can declare it as many time you want.

Soumya
  • 11
  • 2
0

I think Apple's Swift 4 Language Reference can be construed as the authoritative answer. From the Declarations section (emphasis mine):

A declaration introduces a new name or construct into your program. For example, you use declarations to introduce functions and methods, variables and constants, and to define new, named enumeration, structure, class, and protocol types. You can also use a declaration to extend the behavior of an existing named type and to import symbols into your program that are declared elsewhere.

In Swift, most declarations are also definitions in the sense that they are implemented or initialized at the same time they are declared. That said, because protocols don’t implement their members, most protocol members are declarations only. For convenience and because the distinction isn’t that important in Swift, the term declaration covers both declarations and definitions.

toraritte
  • 6,300
  • 3
  • 46
  • 67