How do I create a function prototype in powershell? - powershell

I can't find an example. How do I make a prototype for my powershell functions?
function exampleFunc();
//other stuff that calls the example function
Function exampleFunc(){//stuff}

PowerShell doesn't support prototype functions, forward declarations or whichever term you want to use for this. In PowerShell, when you use the function keyword you're defining a function. If you call it twice with the same function name, you change the function's definition.
This question about the same issue with bash lists the common methods to get around the issue. You can do the same things in PowerShell.
Another option is to use the Begin {} Process {} End {} advanced function construct, and put all your function declarations in the Begin {} portion.

You could call for snippets from Powershell ISE (Integrated Scription Environment) by right click or access via quick buttons Ctrl+J.

And here is what #bacon-bits code would look like
function main()
{
cls
Begin
Process
End
}
function Begin {
}
function Process {
}
function End {
}
main

Related

Lua override class function in included file

I'm sorry I'm new to LUA scripts and I have to work on code written by others.
Please don't focus on code, my problem is only about included files and priority evaluating which function has to be called, in case of overriding.
Let's say I have a file Terrain.lua containing a class Terrain, which has a function Terrain:generate() and Terrain:generate() calls Terrain:getLatitude().
Terrain was included in a script MyScript.lua, which overrided Terrain:getLatitude() as follows:
include("Terrain");
function Terrain:getLatitude()
new code;
end
function myFunction()
local myTerrain = Terrain.create();
myTerrain.generate();
end
This has the effect of overriding getLatitude(): when myTerrain.generate() is called, generate() is the code from the included "Terrain", but getLatitude() is the local function with the new code, even if called by a function from the included class.
Now let's say I want to put some of the code in an external file Custom.lua. Custom (and not MyScript) has to override getLatitude().
This is the situation:
Terrain.lua contains Terrain class and these functions
Terrain.create()
Terrain.generate()
Terrain.getLatitude()
MyScript.lua is the script being executed, and include Custom:
include("Custom");
function myFunction()
return customFunction()
end
Custom.lua contains:
include("Terrain");
function Terrain:getLatitude()
new code;
end
function customFunction()
local myTerrain = Terrain.create();
myTerrain.generate();
end
Now, if I call customFunction() from MyScript, getLatitude() from Terrain is used, instead of getLatitude() from Custom. I assume ovveride is possible only inside the currenti file being executed? How can I achieve overriding in an included file?
I hope this example is enough to understand my problem, without posting a lot of code. Thank you.
Firstly, some corrections: there is no local function's in your question; include is not part of any lua standard, what that function actually does may be quite important.
Finally, Lua does not have actual class system, what you use in the question is merely a syntactic sugar (misleading and confusing as I find it) over table assignments. Lua is an interpreted language, so what may seem to you as a class definition is not a static structure known from the very beginning of the program execution but a code that gets executed from the top of the file to the bottom.
Thus, if we assume that include is similar to the require, then the your question code will be equivalent to the following:
do--terrain.lua
Terrain = {
create=function()
local created_object
--some code to assign value to created_object
return created_object
end
}
Terrain.generate = function(self) end
Terrain.getLatitude = function(this_is_a_self_too)
--some code that uses `also_self` as a reference to the object when called as object:generate()
end
--do end block is essentially an equivalent of file, its local variables are not seen outside
--global variables it has assigned (like `terrain`) will stay accessible AFTER its end
--changes it done to global variables will also remain
end
do--Custom.lua
Terrain.getLatitude = function(this)--this is the assignment to a field in a table stored in the global variable Terrain
--this function will replace the one assigned to the `getLatitude` field
end
customFunction = function()
local myTerrain = Terrain.create();
myTerrain.generate();--this one probably needs `:` instead of `.`
--depends on actual code inside terrain.lua
end
end
do--MyScript.lua
myFunction= function()
return customFunction() --this line calls the global variable customFunction
end
end
Thus if your actual setup is similar to the one in question, then the "override" will take effect after the Custom.lua is executed and for all the subsequent calls to the Terrain.getLatitude regardless of whether or not they've called the file. (And any later file can override it again, and all calls after that will be using the new one)
It is probably more complicated to do a limited override in this setup. That again depends on the actual details of how your team has defined the Terrain class and the class system itself.

Create a function pointer in Game Maker Studio

I would like to dynamically change the script that is executed when a index in an array is called.
The following is valid syntax:
actions[0] = script_do_something
...
actions[n] = script_do_something_else
How do I execute the scripts stored in the array? If I cant, then how else could I replicate a function pointer in GMS?
Unfortunately this doesn't work:
actions[0]()
I would like to avoid if statements as I feel they will get too long and messy.
you should use script_execute function.
script_execute(actions[0]);
also you may pass arguments:
script_execute(actions[0], arg1, arg2);

JRuby: command pattern in Java with Ruby block: why does it work?

I am learning how to integrate Java library with Ruby code and come to the following question.
I have a command pattern implemented in Java, as follows:
public interface Command {
public String execute(String param);
}
public class CommandRunner {
public String run(Command cmd, String param) {
return cmd.execute(param)+" [this is added by run method]";
}
}
When I import that to JRuby program I can implement Ruby class that respond_to? :execute with one parameter and pass it to the CommandRunner.new.run. That works and that's clear.
But I can also do this:
def put_through_runner(param, &block)
CommandRunner.new.run block, param
end
p = put_through_runner "through method" do |param|
"Cmd implementation in block, param: #{param}"
end
puts p
Instead of passing to Java CommandRunner an object implementing the execute method I pass it a block of code, that does not implement the method. And It works: calls the block as if it was implementation of the execute method! How is that possible? What does JRuby do with the block when passing it to Java? If I had the CommandRunner implemented in Ruby the above code would not work.
The reason that this works is a feature called 'closure conversion' (see docs here). What happens is that the block you pass is converted into a Proc object with a proxy that invokes the code in the block for any method that is called on the object.

Can I import a namespace globally without explicitly calling import in each and every function?

In order to avoid namespace bloating, I use packages. For example, let Foo be a function in a package called FooPackage
function Foo()
disp('Foo');
end
I want to use this function in another function called Bar.
function Bar()
InFunc1();
InFunc2();
InFunc3();
end
this function calls sub-functions. The Naive way is to say explicitly the package name in each call
function InFunc1()
FooPackage.Foo();
end
function InFunc2()
FooPackage.Foo();
end
function InFunc3()
FooPackage.Foo();
end
Alternatively I can use an import in each and every function:
function InFunc1()
import FooPackage.*
Foo();
end
function InFunc2()
import FooPackage.*
Foo();
end
function InFunc3()
import FooPackage.*
Foo();
end
Both of the ways are exhausting. The answer in here says that thes are the only ways.
Does anyone has a better suggestion?
Maybe you could use a private directory. The functions in the private directory can be seen only by functions in its parent directory, and they can be called just by their names.
It's not a completely satisfying solution, but that can help.

MobileSubstrate: MSHookFunction example

I am trying to write a MobileSubstrate plugin which hooks into a C-method. I tried to edit the famous "ExampleHook", by just writing a demo MSHook and hook it in the Initialize method.
This is probably too optimistic and it doesn't work. But I cannot find anywhere a simple example of a MSHookFunction(). There is barely information about this on the Internet. It might be possible I misunderstood the whole concept of MSHookFunction.
Please, can anybody help me out with a little example code? I would deeply appreciate any help.
Best regards,
Marc Backes
I realize you have found this, but I am posting this answer to help whoever else may be needing this.
A simple example can be found at the MobileSubstrate article on the iPhone Dev Wiki, and an actual example of this in a project is at this bit of User Agent Faker.
But what is an answer without an actual explanation? Therefore, here we go!
void MSHookFunction(void* function, void* replacement, void** p_original); is the function definition for MSHookFunction, the magic function which causes your function X() to be interposed by Y(), for instance.
That is, when a program commonly would call X(), the call will be redirected to Y() instead. This is pretty much a basic explanation of function interposing.
Now, what are the parameters, and their usefulness?
function is a function pointer to the function you want to interpose. That would be a function pointer to X(), in our quick explanation.
replacement is a function pointer to the function you want function to be interposed with. In our quick explanation, that would be a function pointer to Y().
p_original is a pointer to a function pointer, which from now on will point to what function used to be.
The reason this is there is simple: If you intend to modify behavior, but not suppress it, you'll still need to call what X() used to be. But a common call to X() wouldn't work as intended, as it would end calling Y() instead of the default function.
Therefore, you have a function pointer to call X() as if it wasn't interposed.
Now, explaining the devwiki example:
static void (*original_CFShow)(CFTypeRef obj); // a function pointer to store the original CFShow().
void replaced_CFShow(CFTypeRef obj) { // our replacement of CFShow().
printf("Calling original CFShow(%p)...", obj);
original_CFShow(obj); // calls the original CFShow.
printf(" done.\n");
}
...
// hook CFShow to our own implementation.
MSHookFunction(CFShow, replaced_CFShow, &original_CFShow);
// From now on any call to CFShow will pass through replaced_CFShow first.
...
CFShow(CFSTR("test"));
Here, we:
Pass a pointer to CFShow, the function we want to change default behavior from as the function parameter.
Pass a pointer to the function we just created, replaced_CFShow as the replacement parameter. That is, whenever CFShow would be called by default, replaced_CFShow will be called instead.
We pass a pointer to the original_CFShow function pointer as the p_original parameter. Since we still want the things CFShow still does by itself to be done inside our replacement function, we call it.