Is a function a value type or a reference type in Swift? And Why?
Consider this function:
func addTwoInts(_ a: Int, _ b: Int) -> Int {
return a + b
}
Here I am assigning this function to another variable
var mathFunction: (Int, Int) -> Int = addTwoInts
Here addTwoInts
is a value type or reference type? And why?
swift
add a comment |
Consider this function:
func addTwoInts(_ a: Int, _ b: Int) -> Int {
return a + b
}
Here I am assigning this function to another variable
var mathFunction: (Int, Int) -> Int = addTwoInts
Here addTwoInts
is a value type or reference type? And why?
swift
you can attempt to print memory addresses of both functions stackoverflow.com/questions/24058906/… if same then it's reference otherwise value
– Sh_Khan
Nov 23 '18 at 17:07
add a comment |
Consider this function:
func addTwoInts(_ a: Int, _ b: Int) -> Int {
return a + b
}
Here I am assigning this function to another variable
var mathFunction: (Int, Int) -> Int = addTwoInts
Here addTwoInts
is a value type or reference type? And why?
swift
Consider this function:
func addTwoInts(_ a: Int, _ b: Int) -> Int {
return a + b
}
Here I am assigning this function to another variable
var mathFunction: (Int, Int) -> Int = addTwoInts
Here addTwoInts
is a value type or reference type? And why?
swift
swift
edited Nov 23 '18 at 17:21
Hamish
46.3k7105165
46.3k7105165
asked Nov 23 '18 at 16:38
DamonDamon
522318
522318
you can attempt to print memory addresses of both functions stackoverflow.com/questions/24058906/… if same then it's reference otherwise value
– Sh_Khan
Nov 23 '18 at 17:07
add a comment |
you can attempt to print memory addresses of both functions stackoverflow.com/questions/24058906/… if same then it's reference otherwise value
– Sh_Khan
Nov 23 '18 at 17:07
you can attempt to print memory addresses of both functions stackoverflow.com/questions/24058906/… if same then it's reference otherwise value
– Sh_Khan
Nov 23 '18 at 17:07
you can attempt to print memory addresses of both functions stackoverflow.com/questions/24058906/… if same then it's reference otherwise value
– Sh_Khan
Nov 23 '18 at 17:07
add a comment |
2 Answers
2
active
oldest
votes
I would say that a function type fits best with the definition of a reference type:
Reference types are not copied when they are assigned to a variable or constant, or when they are passed to a function. Rather than a copy, a reference to the same existing instance is used.
However I don't think this is a particularly useful distinction to make, IMO it would also be valid to make the argument that functions are value types (as technically speaking they consist of a pointer and a reference, both of which are copied when the function value is passed about).
The more useful distinction to make is that functions have reference semantics, meaning that it's possible to observe some kind of shared state between instances. This is not the same as being a reference type – it's possible for value types to have reference semantics, and for reference types to have value semantics.
For example here's a value type with reference semantics:
var foo = 0
struct S {
var bar: Int {
get { return foo }
nonmutating set { foo = newValue }
}
}
let s = S()
let s1 = s
s.bar += 1
print(s.bar) // 1
print(s1.bar) // 1
Both s
and s1
share global state, and this is observable by mutating it.
And here's a reference type with value semantics:
final class C {
let foo = 0
}
let c = C()
let c1 = c
print(c.foo) // 0
print(c1.foo) // 0
Although both c
and c1
share state, this isn't directly observable because that shared state is immutable.
So, why do functions have reference semantics? Because they can contain captured variables, which is shared mutable state that can be observed.
For example:
func makeFn() -> () -> Int {
var foo = 0
return {
foo += 1
return foo
}
}
let fn = makeFn()
let fn1 = fn
print(fn()) // 1
print(fn1()) // 2
add a comment |
Closures and functions are Reference types. This is stated in the Swift language documentation: Closures
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53450277%2fis-a-function-a-value-type-or-a-reference-type-in-swift-and-why%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
I would say that a function type fits best with the definition of a reference type:
Reference types are not copied when they are assigned to a variable or constant, or when they are passed to a function. Rather than a copy, a reference to the same existing instance is used.
However I don't think this is a particularly useful distinction to make, IMO it would also be valid to make the argument that functions are value types (as technically speaking they consist of a pointer and a reference, both of which are copied when the function value is passed about).
The more useful distinction to make is that functions have reference semantics, meaning that it's possible to observe some kind of shared state between instances. This is not the same as being a reference type – it's possible for value types to have reference semantics, and for reference types to have value semantics.
For example here's a value type with reference semantics:
var foo = 0
struct S {
var bar: Int {
get { return foo }
nonmutating set { foo = newValue }
}
}
let s = S()
let s1 = s
s.bar += 1
print(s.bar) // 1
print(s1.bar) // 1
Both s
and s1
share global state, and this is observable by mutating it.
And here's a reference type with value semantics:
final class C {
let foo = 0
}
let c = C()
let c1 = c
print(c.foo) // 0
print(c1.foo) // 0
Although both c
and c1
share state, this isn't directly observable because that shared state is immutable.
So, why do functions have reference semantics? Because they can contain captured variables, which is shared mutable state that can be observed.
For example:
func makeFn() -> () -> Int {
var foo = 0
return {
foo += 1
return foo
}
}
let fn = makeFn()
let fn1 = fn
print(fn()) // 1
print(fn1()) // 2
add a comment |
I would say that a function type fits best with the definition of a reference type:
Reference types are not copied when they are assigned to a variable or constant, or when they are passed to a function. Rather than a copy, a reference to the same existing instance is used.
However I don't think this is a particularly useful distinction to make, IMO it would also be valid to make the argument that functions are value types (as technically speaking they consist of a pointer and a reference, both of which are copied when the function value is passed about).
The more useful distinction to make is that functions have reference semantics, meaning that it's possible to observe some kind of shared state between instances. This is not the same as being a reference type – it's possible for value types to have reference semantics, and for reference types to have value semantics.
For example here's a value type with reference semantics:
var foo = 0
struct S {
var bar: Int {
get { return foo }
nonmutating set { foo = newValue }
}
}
let s = S()
let s1 = s
s.bar += 1
print(s.bar) // 1
print(s1.bar) // 1
Both s
and s1
share global state, and this is observable by mutating it.
And here's a reference type with value semantics:
final class C {
let foo = 0
}
let c = C()
let c1 = c
print(c.foo) // 0
print(c1.foo) // 0
Although both c
and c1
share state, this isn't directly observable because that shared state is immutable.
So, why do functions have reference semantics? Because they can contain captured variables, which is shared mutable state that can be observed.
For example:
func makeFn() -> () -> Int {
var foo = 0
return {
foo += 1
return foo
}
}
let fn = makeFn()
let fn1 = fn
print(fn()) // 1
print(fn1()) // 2
add a comment |
I would say that a function type fits best with the definition of a reference type:
Reference types are not copied when they are assigned to a variable or constant, or when they are passed to a function. Rather than a copy, a reference to the same existing instance is used.
However I don't think this is a particularly useful distinction to make, IMO it would also be valid to make the argument that functions are value types (as technically speaking they consist of a pointer and a reference, both of which are copied when the function value is passed about).
The more useful distinction to make is that functions have reference semantics, meaning that it's possible to observe some kind of shared state between instances. This is not the same as being a reference type – it's possible for value types to have reference semantics, and for reference types to have value semantics.
For example here's a value type with reference semantics:
var foo = 0
struct S {
var bar: Int {
get { return foo }
nonmutating set { foo = newValue }
}
}
let s = S()
let s1 = s
s.bar += 1
print(s.bar) // 1
print(s1.bar) // 1
Both s
and s1
share global state, and this is observable by mutating it.
And here's a reference type with value semantics:
final class C {
let foo = 0
}
let c = C()
let c1 = c
print(c.foo) // 0
print(c1.foo) // 0
Although both c
and c1
share state, this isn't directly observable because that shared state is immutable.
So, why do functions have reference semantics? Because they can contain captured variables, which is shared mutable state that can be observed.
For example:
func makeFn() -> () -> Int {
var foo = 0
return {
foo += 1
return foo
}
}
let fn = makeFn()
let fn1 = fn
print(fn()) // 1
print(fn1()) // 2
I would say that a function type fits best with the definition of a reference type:
Reference types are not copied when they are assigned to a variable or constant, or when they are passed to a function. Rather than a copy, a reference to the same existing instance is used.
However I don't think this is a particularly useful distinction to make, IMO it would also be valid to make the argument that functions are value types (as technically speaking they consist of a pointer and a reference, both of which are copied when the function value is passed about).
The more useful distinction to make is that functions have reference semantics, meaning that it's possible to observe some kind of shared state between instances. This is not the same as being a reference type – it's possible for value types to have reference semantics, and for reference types to have value semantics.
For example here's a value type with reference semantics:
var foo = 0
struct S {
var bar: Int {
get { return foo }
nonmutating set { foo = newValue }
}
}
let s = S()
let s1 = s
s.bar += 1
print(s.bar) // 1
print(s1.bar) // 1
Both s
and s1
share global state, and this is observable by mutating it.
And here's a reference type with value semantics:
final class C {
let foo = 0
}
let c = C()
let c1 = c
print(c.foo) // 0
print(c1.foo) // 0
Although both c
and c1
share state, this isn't directly observable because that shared state is immutable.
So, why do functions have reference semantics? Because they can contain captured variables, which is shared mutable state that can be observed.
For example:
func makeFn() -> () -> Int {
var foo = 0
return {
foo += 1
return foo
}
}
let fn = makeFn()
let fn1 = fn
print(fn()) // 1
print(fn1()) // 2
edited Nov 23 '18 at 17:30
answered Nov 23 '18 at 17:15
HamishHamish
46.3k7105165
46.3k7105165
add a comment |
add a comment |
Closures and functions are Reference types. This is stated in the Swift language documentation: Closures
add a comment |
Closures and functions are Reference types. This is stated in the Swift language documentation: Closures
add a comment |
Closures and functions are Reference types. This is stated in the Swift language documentation: Closures
Closures and functions are Reference types. This is stated in the Swift language documentation: Closures
answered Nov 23 '18 at 17:08
CPRCPR
26819
26819
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53450277%2fis-a-function-a-value-type-or-a-reference-type-in-swift-and-why%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
you can attempt to print memory addresses of both functions stackoverflow.com/questions/24058906/… if same then it's reference otherwise value
– Sh_Khan
Nov 23 '18 at 17:07