Skip to content

Commit

Permalink
Improve and test support for custom functions
Browse files Browse the repository at this point in the history
* NamesLocalVariablesFunction actually is a kernel operation, mark it as such
* fix a crash caused by infinite recursion when trying to display a function
  which is not a `T_FUNCTION`
* add tests for installing custom methods for NameFunction,
  NamesLocalVariablesFunction, and NumberArgumentsFunction
  • Loading branch information
fingolfin committed Apr 30, 2018
1 parent d0693f8 commit cda79cf
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 5 deletions.
2 changes: 1 addition & 1 deletion lib/function.g
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ DeclareOperationKernel( "NumberArgumentsFunction", [IS_OBJECT], NARG_FUNC );
## </ManSection>
## <#/GAPDoc>
##
BIND_GLOBAL( "NamesLocalVariablesFunction", NAMS_FUNC );
DeclareOperationKernel( "NamesLocalVariablesFunction", [IS_OBJECT], NAMS_FUNC );


#############################################################################
Expand Down
3 changes: 3 additions & 0 deletions lib/function.gi
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@ end);
InstallMethod(DisplayString, "for a function, using string stream", [IsFunction],
function(fun)
local s, stream;
if TNUM_OBJ(fun) <> T_FUNCTION then
TryNextMethod();
fi;
s := "";
stream := OutputTextString(s, true);
PrintTo(stream, fun);
Expand Down
16 changes: 12 additions & 4 deletions tst/testinstall/callfunc.tst
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,20 @@ gap> ForAll([0..100], x -> CallFuncListWrap(swallow, List([1..x], y -> [y]) ) =
true

# test overloading CallFuncList
gap> fam := NewFamily("XYZsFamily");;
gap> cat := NewCategory("IsXYZ",IsObject);;
gap> fam := NewFamily("CustomFunctionFamily");;
gap> cat := NewCategory("IsCustomFunction", IsFunction);;
gap> type := NewType(fam, cat and IsPositionalObjectRep);;
gap> result := fail;;
gap> InstallMethod(CallFuncList,[cat,IsList],function(func,args) result:=args; return args; end);
gap> o := Objectify(type,[]);;
gap> InstallMethod(NameFunction, [cat], f -> f![1]);
gap> InstallMethod(NamesLocalVariablesFunction, [cat], f -> ["arg"]);
gap> InstallMethod(NumberArgumentsFunction, [cat], f -> -1);

#
gap> o := Objectify(type,["myName"]);
function( arg... ) ... end
gap> Display(o);
<object>

# test dispatch through interpreter / IntrFuncCallEnd
gap> o();
Expand Down Expand Up @@ -109,7 +117,7 @@ gap> CallFuncList(o, [1,2,3,4,5,6,7]);
[ 1, 2, 3, 4, 5, 6, 7 ]

# test overloading CallFuncList with a procedure call
gap> cat2 := NewCategory("IsXYZ2",IsObject);;
gap> cat2 := NewCategory("IsCustomFunction2",IsFunction);;
gap> type2 := NewType(fam, cat2 and IsPositionalObjectRep);;
gap> InstallMethod(CallFuncList,[cat2,IsList],function(func,args) result:=args; end);
gap> o2 := Objectify(type2,[]);;
Expand Down

0 comments on commit cda79cf

Please sign in to comment.