Re: [Vala] Confusing closure behavior
- From: Nor Jaidi Tuah <norjaidi tuah ubd edu bn>
- To: <vala-list gnome org>
- Subject: Re: [Vala] Confusing closure behavior
- Date: Tue, 9 Apr 2019 17:23:58 +0800
On Tue, 2019-04-09 at 08:25 +0200, Vivien Kraus wrote:
Digging a little deeper, I have some more confusion. This makes the
for-loop and the foreach-loop behave differently. What do you think
of:
// capturing the for variable should capture a copy if the variable
is
// local to the for loop
public delegate void Thunk();
int main() {
Thunk thunk = () => {};
for (int i = 0; i < 1; i++) {
thunk = () => {
stderr.printf ("%d = 0, right?\n", i);
};
}
thunk ();
int[] indices = { 0 };
foreach (var i in indices) {
thunk = () => {
stderr.printf ("%d = 0, right?\n", i);
};
}
thunk ();
return 0;
}
I believe foreach pass the values inside the array.
e.g.,
foreach (var i in indices) {
i = 999;
}
will not change indices[0] at all. A more strict language
will even forbid that "i = 999".
Nice day
Nor Jaidi Tuah
PRIVILEGED/CONFIDENTIAL information may be contained in this message. If you are neither the addressee
(intended recipient) nor an authorised recipient of the addressee, and have received this message in error,
please destroy this message (including attachments) and notify the sender immediately. STRICT PROHIBITION:
This message, whether in part or in whole, should not be reviewed, retained, copied, reused, disclosed,
distributed or used for any purpose whatsoever. Such unauthorised use may be unlawful and may contain
material protected by the Official Secrets Act (Cap 153) of the Laws of Brunei Darussalam. DISCLAIMER:
We/This Department/The Government of Brunei Darussalam, accept[s] no responsibility for loss or damage
arising from the use of this message in any manner whatsoever. Our messages are checked for viruses but we do
not accept liability for any viruses which may be transmitted in or with this message.
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]