Call global function from within Delphi class's method - delphi

Is it possible to call global methods from within a class where they are obscured by member functions of the same name?
I know in C++ you have the following syntax:
int var = 0;
void temp() {
int var = 2;
::var = var;
} //Global var is set to 2

Yes you can by using the name of the unit instead of ::
Like:
unit1.var := 2;
See for more details:
http://delphi.about.com/od/beginners/l/aa060899.htm

You can try
UnitName.VarName := 2

Related

Accept object as an function

Is there any way in Dart to accept an object as a function?
I am looking for some operator or type that can replace +/*()=>*/ with some other operator without changing the expected functionality if that those operators would have not been commented
For example:
class Test {
operator +([...])=>[...];
}
void main() {
Test test = Test();
int a = 3;
var func = test + /*()=>*/ a;
print(func()); // should print 3
++a;
print(func()); // should print 4
}

F# assign value to a method argument

I am trying to override a method provided by an interface/API and have to assign a new value to one of the method's argument. If I try to assign to the passed argument, it'll give an error.
override _.Emit(eventInfo:SequenceStartEventInfo, emitter:IEmitter) =
eventInfo <- SequenceStartEventInfo(eventInfo.Source)
[...]
I am looking for behavior matching the following C# code:
public override void Emit(SequenceStartEventInfo eventInfo, IEmitter emitter) {
eventInfo = new SequenceStartEventInfo(eventInfo.Source)
...
}
If I change it and try to pass by reference (eventInfo:byref<SequenceStartEventInfo>) then it'll no longer match the available overloads.
The F# language reference on parameters and methods doesn't provide any help when dealing with this specific case. What is the best way to handle this Scenario?
In C# you can assign a new value to the argument variable, but as it is not passed by reference, this won't change the variable of the caller (even though the passed object is a reference type):
public static void Main()
{
var rt = new RefType { Value = 3 };
Change(rt);
Console.WriteLine(rt.Value); // still 3
}
public class RefType {
public int Value { get; set; }
}
public static void Change(RefType notByRef){
notByRef = new RefType { Value = 42 };
}
the F# equivalent (where parameters are immutable) would be shadowing:
type RefType() =
member val Value = 0 with get, set
let Change notByRef =
let notByRef = RefType(Value = 42);
// now, `notByRef` hides the method parameter
()
let [<EntryPoint>] Main _ =
let rt = RefType(Value = 3);
Change(rt);
printfn "%i" rt.Value // still 3
0

How to compare objects in Vala?

I am using a Gee.ArrayList with an own class for content. I want to use the "contains" method of the ArrayList, but I really don't know how to set up an equals-method in my class, so ArrayList uses it to find out if the object is in the ArrayList or not.
Example:
class Test : GLib.Object {
public int number;
public Test(int n) {
number = n;
}
public bool equals (Test other) {
if (number == other.number) return true;
return false;
}
}
Then, in another file:
var t = new Gee.ArrayList<Test>();
var n1 = new Test(3);
var n2 = new Test(3);
t.add(n1);
t.contains(n2); // returns false, but I want it to return true
Does anybody know that?
When you create the ArrayList, the constructor takes your equality comparator. You can do:
var t = new Gee.ArrayList<Test>(Test.equals);
and the contains should work as you desire.

Does vala have function static variables?

Does Vala have function static variables?
By "function static variable" I mean a variable declared inside a function that keeps its value between invocations, like in the following c example:
#include <stdio.h>
void foo()
{
int a = 10;
static int sa = 10;
a += 5;
sa += 5;
printf("a = %d, sa = %d\n", a, sa);
}
No, it doesn't.
In your example you can either use a global variable or wrap the function in a class and make the variable an attribute of that class.
The keyword static has a completely different meaning and is only used for class members that are not bound to an instance.

How to call a function using pointer in js-ctypes

I only have a pointer to a function, how to call it in js-ctypes?
Thanks.
If you got a function pointer from a C function then you need to make sure it's correctly interpreted as a pointer to FunctionType. Then you can simply call it as you would a JavaScript function. For example, GetProcAddress() returns a function pointer - in the following code I declare GetProcAddress() with a void pointer as return type, then I cast that pointer to a function type matching the signature of the MessageBox() function:
Components.utils.import("resource://gre/modules/ctypes.jsm");
var BOOL = ctypes.int32_t;
var HANDLE = ctypes.voidptr_t;
var HMODULE = HANDLE;
var HWND = HANDLE;
var FARPROC = ctypes.voidptr_t;
var LPCTSTR = ctypes.jschar.ptr;
var LPCSTR = ctypes.char.ptr;
var kernel = ctypes.open("kernel32.dll");
var LoadLibrary = kernel.declare(
"LoadLibraryW",
ctypes.winapi_abi,
HMODULE, // return type
LPCTSTR // parameters
);
var FreeLibrary = kernel.declare(
"FreeLibrary",
ctypes.winapi_abi,
BOOL, // return type
HMODULE // parameters
);
var GetProcAddress = kernel.declare(
"GetProcAddress",
ctypes.winapi_abi,
FARPROC, // return type
HMODULE, LPCSTR // parameters
);
// Load the library we're interested in.
var hUser = LoadLibrary("user32");
// Get the pointer to the function.
var MessageBox = GetProcAddress(hUser, "MessageBoxW");
// Now we have a pointer to a function, let's cast it to the right type.
var MessageBoxType = ctypes.FunctionType(
ctypes.winapi_abi,
ctypes.int32_t, // return type
[HWND, LPCTSTR, LPCTSTR, ctypes.uint32_t] // parameters
);
MessageBox = ctypes.cast(MessageBox, MessageBoxType.ptr);
// Actually call the function.
MessageBox(null, "Test1", "Test2", 0);
// Free the library again if no longer needed. Any imported function
// pointers should be considered invalid at this point.
FreeLibrary(hUser);

Resources