Function Result is Generic - function

I need something like this:
function fn_get_all_propperties (obj : TObject) : TObjectList<TTypeKind>;
But:
[DCC Error] uFuncMain.pas(20): E2511 Type parameter 'T' must be a class type
What type should be the result of a function?

The problem is that TObjectList is defined as follows:
TObjectList<T: class> = class(TList)
....
end;
The T: class in the definition means that the generic parameter T is constrained to be a class. But TTypeKind is not a class. It is a value type.
So the compiler rejects your attempted generic instantiation as being invalid because it does not satisfy the constraint.
So you cannot use TObjectList<T> here and instead should use TList<T>. Your function should be defined like this:
function fn_get_all_properties(obj: TObject): TList<TTypeKind>;

Related

Understanding the difference between object.function(argument) and object:function(argument) in Lua

obj ={x=30}
function obj:printpos()
print(self.x)
end
other = {x=15}
obj.printpos(other)
The obj.printpos(other) gives expected output viz. 15. However the call obj:printpos(other) doesn't give expected output.It still prints 30. Why the call obj:printpos(other) is not taking other as its argument? Basically what is the difference between object.function(argument) and object:function(argument)? Is object:function(argument) same as object:function() i.e. whether argument is ignored?
obj:printpos(other) is equivalent to obj.printpos(obj, other).
function obj:printpos() end is equivalent to function obj.printpos(self) end.
From Lua 5.4 Reference Manual - §3.4.11 – Function Definitions (formatting mine):
The colon syntax is used to emulate methods, adding an implicit extra parameter self to the function. Thus, the statement
function t.a.b.c:f (params) body end
is syntactic sugar for
t.a.b.c.f = function (self, params) body end
From this, we can see the colon syntax implicitly adds the self parameter to the function scope.
Inversely, calling a function defined with the colon syntax using the dot syntax will cause the first argument passed to it to be assigned to the self parameter.
Thus, with
local thing = {}
function thing:func()
print(self)
end
the calls
thing:func()
-- and
thing.func(thing)
have the same result of assigning thing to self, and
thing.func(other_thing)
will assign other_thing to self.
The problem with
thing:func(other_thing)
is that, as previously seen, thing is assigned to self. other_thing is assigned to no parameter, as no other parameters were defined.

CoffeeScript idiom to either call function or property getter

An objects property can be a simple property or a function. Is there some easier way in CoffeeScript to get the value of this property?
value = if typeof obj.property is "function" then obj.property() else obj.property
I don't know if it is idiomatic but you could use (abuse?) the existential operator for this purpose.
When you say this:
obj.p?()
# ---^
CoffeeScript will convert that to:
typeof obj.p === "function" ? obj.p() : void 0
so if p is a function, it will be called, otherwise you get undefined. Then you can toss in another existential operator to fall back to obj.p if obj.p?() is undefined:
obj.p?() ? obj.p
There is a whole in this though, if you have:
obj =
u: -> undefined
then obj.u?() ? obj.u will give you the whole function back rather than the undefined that the function returns. If you have to face that possibility then I think you're stuck writing your own function:
prop = (x) ->
# Argument handling and preserving `#` is left as an exercise
if typeof x == 'function'
x()
else
x
and saying x = prop obj.maybe_function_maybe_not.
Demo: http://jsfiddle.net/ambiguous/hyv6pdtc/
If you happen to have Underscore around, you could use it's result function:
result _.result(object, property)
If the value of the named property is a function then invoke it with the object as context; otherwise, return it.

Delphi function generic

I would like to create a generic function. I'm novice in generic.
I've 3 private lists of different type. I want a public generic method for return 1 item of the list.
I've the code below. (I have it simplifie)
TFilter = class
private
FListFilter : TObjectList<TFilterEntity>;
FListFilterDate : TObjectList<TFilterDate>;
FListFilterRensParam : TObjectList<TFilterRensParam>;
public
function yGetFilter<T>(iIndice : integer) : T;
....
function TFilter .yGetFilter<T>(iIndice : integer) : T;
begin
if T = TFilterEntity then
result := T(FListFilter.Items[iIndice])
else
....
end;
I know that code doesn't run, but can you tell me if it's possible to do a thing that it ?
Just introduce a constraint of the generic parameter T. It has to be a class.
From the documentation:
A type parameter may be constrained by zero or one class type. As with interface type constraints, this declaration means that the compiler will require any concrete type passed as an argument to the constrained type param to be assignment compatible with the constraint class.
Compatibility of class types follows the normal rules of OOP type compatibilty - descendent types can be passed where their ancestor types are required.
Change declaration to:
function yGetFilter<T:class>(iIndice : integer) : T;
Update
It appears that in XE5 and earlier you get a compiler error:
E2015 Operator not applicable to this operand type
at this line:
if T = TFilterEntity then
In XE6 and above this bug is fixed.
To circumvent, do as David says in a comment:
if TClass(T) = TFilterEntity then

VFP function to check if a method is defined

I would like to trigger a method of an object by using BINDEVENT(), but the method may not exist. Thus, I want to check if the method is defined before issuing BINDEVENT().
For example, in the following code snippet, if oHandler.myresize() does not exist, the error will be triggered at the line of BINDEVENT().
PUBLIC oHandler
oHandler=NEWOBJECT("myhandler")
DO (_browser)
BINDEVENT(_SCREEN,"Resize",oHandler,"myresize")
DEFINE CLASS myhandler AS Session
PROCEDURE myresize
IF ISNULL(_obrowser) THEN
UNBINDEVENTS(THIS)
ELSE
_obrowser.left = _SCREEN.Width - _obrowser.width
ENDIF
RETURN
ENDDEFINE
Thus, I want to check the method myresize() exists or not.
Is there any language function for this purpose? It is very similar to a php function function_exits() or method_exists().
PEMSTATUS( VariableNameRepresentingTheObject, "MethodOrPropertyLookingFor", 5 )
returns true or false if exists on the given object.

Is there a way to determine which optional arguments were given in a VBA function call?

Say I have a VBA function with an optional argument. Is there a way to tell, from within that function, whether the calling code has supplied the optional argument or not?
Public Function SomeFunction(optional argument as Integer = 0) As Integer
End Function
i.e. Is there a way to tell the difference between the following calls?
x = SomeFunction()
x = SomeFunction(0)
As far as I am aware it is not possible. If no argument is passed then the argument is initiliazed to its default value (0 in this case).
One way around this is to change the variable type to Variant and use the IsMissing function to check whether an argument is passed or not. Example:
Public Function SomeFunction(Optional argument As Variant) As Integer
If IsMissing(argument) Then
argument = 0
Else
// Code if argument not = 0....
End If
End Function
The IsMissing function works only with the Variant data type as any other data type will always have a default initialization value.
No, there is not.
The same problem exists in other language, such as C#, as expressed clearly in "C# In Depth", Chapter 13.1.1 (I read this part 15mins ago, it's normal I remember this!)
What I suggest you is not to set the default value in the function declaration. In the function, if argument is null, you set it to 0 and you know it wasn't supplied in the function call.
Edit : Just like #Remnant said in his answer, to be able to do this, the parameter type needs to be a variant.