List.ForEach in vb.net - perplexing me - vb.net

Consider the following code example:
TempList.ForEach(Function(obj)
obj.Deleted = True
End Function)
And this one:
TempList.ForEach(Function(obj) obj.Deleted = True)
I would expect the results to be the same, however the second code example does NOT change the objects in the list TempList.
This post is more to understand why...? Or at least get some help understanding why...

It's because you used Function instead of Sub. Since a Function returns a value, the compiler considers that the equals sign (=) is used as a comparison, not an assignment. If you change Function to Sub, the compiler would correctly consider the equals sign as an assignment:
TempList.ForEach(Sub(obj) obj.Deleted = True)
If you had a multiline lambda; you wouldn't have had this problem:
TempList.ForEach(Function(obj)
obj.Deleted = True
Return True
End Function)
Obviously, for the ForEach method it makes no sense to use a Function because the return value wouldn't be used, so you should use a Sub.

Related

What to use as metatable?

Reading http://lua-users.org/wiki/LuaClassesWithMetatable the Vector example uses the following technique for the metatable:
Vector={}
Vector_mt={__index=Vector}
function Vector:new(x,y)
return setmetatable({x=x,y=y},Vector_mt)
end
function Vector:add(v)
return Vector:new(self.x+v.x,self.y+v.y)
end
Supposing we want to use __add to support the + operator, we need to explicitly mention it in the metatable, and also we need to reorder things, so that the metatable and the constructor are mentioned after Vector:add:
Vector={}
function Vector:add(v)
return Vector:new(self.x+v.x,self.y+v.y)
end
Vector_mt={__index=Vector,__add=Vector.add}
function Vector:new(x,y)
return setmetatable({x=x,y=y},Vector_mt)
end
To avoiding having to mention each metamethod in the metatable, I can set Vector itself as the metatable, then I can add __add (plus, obviously, __index) as a method of Vector:
Vector={}
function Vector:new(x,y)
return setmetatable({x=x,y=y},Vector)
end
function Vector:add(v)
return Vector:new(self.x+v.x,self.y+v.y)
end
function Vector:__index(k)
return Vector[k]
end
function Vector:__add(b)
return self:add(b)
end
is the latter not recommended and why?
and also we need to reorder things, so that the metatable and the constructor are mentioned after Vector:add
No. A table is a reference. On adding metatable, it is not deep copied. You can add fields to it later, and it will affect the metatable.
This is over-complicated:
function Vector:__index(k)
return Vector[k]
end
Simply do this:
Vector.__index = Vector
See https://codereview.stackexchange.com/a/253022/230923 for an example of a Lua "class".

Kotlin Filter - The Expression is Unused

I have a Kotlin application that filters a collection elements in a manner similar to the following:
fun main() {
val strings = arrayOf("A", "B", "C")
val acceptAll = true
println(
strings.filter {
if (acceptAll) {
true // "The expression is unused"
}
// Other filters
false
}
)
}
However, the true line is highlighted by IntelliJ with the message "The expression is unused". Sure enough, my program does not hit that code path, the filter returns false, and I end up with an empty array.
This is a simplified example, but it illustrates what it is that I'm trying to accomplish.
Any suggestions would be greatly appreciated.
Thank you!
To actually return the value true from the lambda inside that if statement, you have to replace true with return#filter true.
The statement true does not mean "return true", it only means that when evaluated, the result of the evaluation will be true. Only the last statement of a lambda is considered the return value. And since true was not the last statement, it was not returned. After evaluating true, execution jumped out of the if-case body and executed the next statement, which is false. Since that is also the last statement in the lambda, it was also the return value of the lambda, even though it executed the body of the if statement just before that.
You can leave the statement false without changes. Since it is the last statement (contrary to true) in the lambda, it will also be the return value from the lambda if that statement is executed. For symmetry, I would personally change it to return#filter false, however.
The docs on this can be found at Returning a value from a lambda expression and Return at Labels

Key input functions for a VB class file

I am working on a little framework and I want to have a class file that contains functions I can run to check if a certain key has been pressed so other events can be run. The only code I have found online for similar things are written into the form itself and use something like "Handles Me.KeyPress". However, this handle function can't be used in a class file.
Public Function OnKeyPress(KeyToCheck As Keys)
If KeyPressed = KeyToCheck then
return true
else
return false
End If
End Function
I have tried:
Public Function OnKeyPress(KeyToCheck As Keys)Handles Formname.Keypress
If KeyPressed = KeyToCheck then
return true
else
return false
End If
End Function
However, this does not work. Any suggestions or work arounds would be greatly appreciated.
To get keyboard input, you need to have a window. All input goes to a window, and that window can then check for key presses that are sent to it.
To get global information, you'd need to install a hook, but those should generally be avoided.

Can I dynamically call a local function?

I have a module that looks like this. I've simplified it greatly so as not to clutter up my question (my local functions are more complex than this). Here is my code:
decision = {}
function win(win_arg) return win_arg end
function lose(lose_arg) return lose_arg end
local function draw(draw_arg) return draw_arg end
function decision.get_decision(func, arg)
return func(arg)
end
return decision
I am calling my module with the code below.
my = require "my-lua-script"
print(my.get_decision(lose, "I lose."))
print(my.get_decision(win, "I win."))
I want 'get_decision' to be a public method. I want win, lose and draw to be private, but I want to call them dynamically from get_decision. If I understand correctly, win and lose are in the global namespace right now? If I put a local in front of these two methods (like draw) then my code doesn't work.
Is there a way to accomplish what I am after?
Of course.
my-script.lua
-- This is the local side of the module.
local outcomes = {}
function outcomes.win(arg) return arg end
function outcomes.lose(arg) return arg end
function outcomes.draw(arg) return arg end
-- This is the exposed side of the module.
local M = {}
function M.get_decision(outcome, arg)
return outcomes[outcome](arg)
end
return M
main.lua
local my = require'my-script'
print(my.get_decision('win', 'I win.'))
print(my.get_decision('lose', 'I lose.'))
You simply use a string to indicate which function you'd like to access, and use that string to index a table of functions from within get_decision against outcomes. This will keep the functions hidden behind get_decision.

Trying to exit a method, using return, but doesn't actually return anything

I'm using return EXIT_SUCCESS to exit a void function. However I get a flag that says my method should not return a value.
The code works fine, but is there a better way to exit a method by using a return or something else?
To add to the other answers, you can always return from a void function with return;
Redefine the method as
-(int)myMethod;
Then you can return EXIT_SUCCESS.
Is any code calling this method and expecting a value?
I don't think you understand how functions work. When you specify the prototype asvoid you are saying the function won't return anything. You are then contradicting the prototype by trying to return something. Change the prototype to -(int)function to return EXIT_SUCCESS. If you do not want to return something, but want to still check if it was successful you could pass in a pointer to the function: -(void)functionReturnValue:int* returnValue. Make the function set returnValue to EXIT_SUCCESS on successful completion and have the calling function check returnValue.