Indexing into Array of Functions: Expression Resolves to an Unused L-Value

Indexing into array of functions: Expression resolves to an unused l-value

chartsArray[0] is analagous to writing just the result of lineChart(); you're identifying a value, but not actually doing anything with it.

Expression resolves to unused I Value

AFAIK, this isn't possible the way you are trying to do it.

One option is to loop through all of your buttons and change the background color in the loop.

for button in [button1, button2]{
button.backgroundColor = UIColor.blueColor()
}

The reason you are getting this error is because this line

let chicken1 = button1; button2

is the same as

let chicken1 = button1
button2 //this value isn't used

Swift doesn't actually need ; at the end of a line like in other languages. By adding ;, you tell Swift that you want to have multiple expressions on a single line.

The second line doesn't do anything because there are no function calls and no assignment, so it's like you want to get the value of button2 but you don't use it.

Im getting this error: “ braced block of statements is an unused closure, expected expression and expression resolves to an unused function

You have typed it a bit wrong, try this instead:

if let uid = KeychainWrapper.standard.string(forKey: "uid") {

}

You either need to assign the if let to a variable name or use _.

Order of evaluation of array indices (versus the expression) in C

Order of Left and Right Operands

To perform the assignment in arr[global_var] = update_three(2), the C implementation must evaluate the operands and, as a side effect, update the stored value of the left operand. C 2018 6.5.16 (which is about assignments) paragraph 3 tells us there is no sequencing in the left and right operands:

The evaluations of the operands are unsequenced.

This means the C implementation is free to compute the lvalue arr[global_var] first (by “computing the lvalue,” we mean figuring out what this expression refers to), then to evaluate update_three(2), and finally to assign the value of the latter to the former; or to evaluate update_three(2) first, then compute the lvalue, then assign the former to the latter; or to evaluate the lvalue and update_three(2) in some intermixed fashion and then assign the right value to the left lvalue.

In all cases, the assignment of the value to the lvalue must come last, because 6.5.16 3 also says:

… The side effect of updating the stored value of the left operand is sequenced after the value computations of the left and right operands…

Sequencing Violation

Some might ponder about undefined behavior due to both using global_var and separately updating it in violation of 6.5 2, which says:

If a side effect on a scalar object is unsequenced relative to either a different side effect on the same scalar object or a value computation using the value of the same scalar object, the behavior is undefined…

It is quite familiar to many C practitioners that the behavior of expressions such as x + x++ is not defined by the C standard because they both use the value of x and separately modify it in the same expression without sequencing. However, in this case, we have a function call, which provides some sequencing. global_var is used in arr[global_var] and is updated in the function call update_three(2).

6.5.2.2 10 tells us there is a sequence point before the function is called:

There is a sequence point after the evaluations of the function designator and the actual arguments but before the actual call…

Inside the function, global_var = val; is a full expression, and so is the 3 in return 3;, per 6.8 4:

A full expression is an expression that is not part of another expression, nor part of a declarator or abstract declarator…

Then there is a sequence point between these two expressions, again per 6.8 4:

… There is a sequence point between the evaluation of a full expression and the evaluation of the next full expression to be evaluated.

Thus, the C implementation may evaluate arr[global_var] first and then do the function call, in which case there is a sequence point between them because there is one before the function call, or it may evaluate global_var = val; in the function call and then arr[global_var], in which case there is a sequence point between them because there is one after the full expression. So the behavior is unspecified—either of those two things may be evaluated first—but it is not undefined.

In Swift, can I use function type in tuple?

You could do:

typealias TupleType = (Int, Bool, () -> Void)
var list = [TupleType]()

Unfortunately, trying to access an item in a tuple from the array
results in Playgrounds breaking - 'Communication with the Playground service was unexpectedly interupted'. Trying the same thing in a project results in a segmentation fault. If you're getting the same problem I would recommend you use a struct instead:

struct MyStruct {
let num: Int
let bool: Bool
let closure: () -> Void

init(num: Int, bool: Bool, closure: () -> Void = {}) {
self.num = num
self.bool = bool
self.closure = closure
}
}

var list = [MyStruct]()
list.append(MyStruct(num: 1, bool: true, closure: { println("Hello") }))
list.append(MyStruct(num: 2, bool: false))

Cannot apply indexing with [] to an expression of type 'System.Collections.Generic.IEnumerable

Because it's not.

Indexing is covered by IList. IEnumerable means "I have some of the powers of IList, but not all of them."

Some collections (like a linked list), cannot be indexed in a practical way. But they can be accessed item-by-item. IEnumerable is intended for collections like that. Note that a collection can implement both IList & IEnumerable (and many others). You generally only find IEnumerable as a function parameter, meaning the function can accept any kind of collection, because all it needs is the simplest access mode.

Expression must be a modifiable lvalue

The assignment operator has lower precedence than &&, so your condition is equivalent to:

if ((match == 0 && k) = m)

But the left-hand side of this is an rvalue, namely the boolean resulting from the evaluation of the sub­expression match == 0 && k, so you cannot assign to it.

By contrast, comparison has higher precedence, so match == 0 && k == m is equivalent to:

if ((match == 0) && (k == m))

How to rearrange an array by indices array?

This is the "sign bit" solution.

Given that this is a JavaScript question and the numerical literals specified in the ind array are therefore stored as signed floats, there is a sign bit available in the space used by the input.

This algorithm cycles through the elements according to the ind array and shifts the elements into place until it arrives back to the first element of that cycle. It then finds the next cycle and repeats the same mechanism.

The ind array is modified during execution, but will be restored to its original at the completion of the algorithm. In one of the comments you mentioned that this is acceptable.

The ind array consists of signed floats, even though they are all non-negative (integers). The sign-bit is used as an indicator for whether the value was already processed or not. In general, this could be considered extra storage (n bits, i.e. O(n)), but as the storage is already taken by the input, it is not additional acquired space. The sign bits of the ind values which represent the left-most member of a cycle are not altered.

Edit: I replaced the use of the ~ operator, as it does not produce the desired results for numbers equal or greater than 231, while JavaScript should support numbers to be used as array indices up to at least 232 - 1. So instead I now use k = -k-1, which is the same, but works for the whole range of floats that is safe for use as integers. Note that as alternative one could use a bit of the float's fractional part (+/- 0.5).

Here is the code:

var arr = ["A", "B", "C", "D", "E", "F"];var ind = [4, 0, 5, 2, 1, 3];
rearrange(arr, ind);
console.log('arr: ' + arr);console.log('ind: ' + ind);
function rearrange(arr, ind) { var i, j, buf, temp; for (j = 0; j < ind.length; j++) { if (ind[j] >= 0) { // Found a cycle to resolve i = ind[j]; buf = arr[j]; while (i !== j) { // Not yet back at start of cycle // Swap buffer with element content temp = buf; buf = arr[i]; arr[i] = temp; // Invert bits, making it negative, to mark as visited ind[i] = -ind[i]-1; // Visit next element in cycle i = -ind[i]-1; } // dump buffer into final (=first) element of cycle arr[j] = buf; } else { ind[j] = -ind[j]-1; // restore } }}


Related Topics



Leave a reply



Submit