Forum OpenACS Development: pl/pgsql porting style question

Collapse
Posted by Dave Bauer on
It's me again.

I am still working on static-pages. I have come to come function calls that have several values that are passed in as NULLs.  But when I try to pass them as NULLs the parsers gets confused and can't find the function.

Is it better to create an overloaded function that just needs the specific parameters I am passing?

Collapse
Posted by Don Baccus on
If the combinations of parameters you're seeing are repeated reasonably often, and/or if the NULLs are the "obviously normal default choice" (whatever the heck that means), then yeah, I think overloading as you describe is a very good approach.

On the other hand, making overloaded functions for exceptional cases probably just leads to cluttering the code unnecessarily.

I don't think there's a single, fits-all-cases answer here.  It's a judgement call, so do what you think is more straightforward and more readable for the individual cases you're looking at.

Collapse
Posted by Roberto Mello on
Uh oh. I ported a function call (in content::new_item) in cms/tcl/form-procs.tcl exactly like that last night. I'm not sure how to do it another way.

If someone would like to take a look and give me some ideas, that'd be really nice. You can see the diff right here: https://openacs.org/sdm/entry-history-diff.tcl?current_entry=FILE+%2Fopenacs-4%2Fpackages%2Fcms%2Ftcl+form-procs.tcl&new_release=1.6&package_id=9&old_release=1.5

Collapse
Posted by Dave Bauer on
My main question is if this is the only way to fix the problem where all the stuff I am not using is set to NULL, causing pl/pgsql to not find the function.
Collapse
Posted by Vinod Kurup on
I think when you call a function with a NULL param, pl/pgsql treats the param as type 'unknown' and matches it to any type in the function's parameter list. The problem comes when you have so many NULLs that more than 1 function would match. In that case, you can cast your NULL so that the proper function is called.

Example:

create function vk_test(integer,varchar) returns integer as ' begin raise notice ''integer first function''; return 0; end;' language 'plpgsql'; create function vk_test(varchar,integer) returns integer as ' begin raise notice ''varchar first function''; return 0; end;' language 'plpgsql'; # select vk_test(1, NULL); NOTICE: integer first function # select vk_test(NULL, 1); NOTICE: varchar first function # select vk_test('a'::varchar, NULL); NOTICE: varchar first function # select vk_test(NULL, 'a'::varchar); NOTICE: integer first function # select vk_test(NULL, NULL); ERROR: Function 'vk_test(unknown,unknown)' does not exist # select vk_test(NULL::integer, NULL::varchar); NOTICE: integer function
Using explicit casting of nulls allows you to choose the function with the signature that you want.
Collapse
Posted by Don Baccus on
Hope I didn't cause a misunderstanding, Roberto, in general I like the overloading solution, particularly for common cases.

The only concern - probably not much of one for most functions - is that a proliferation of overloaded functions causes the code to be cluttered with a potentially confusing number of them.

Also, I didn't realize that Dave was asking if there's another solution (he asked if it were better to create an overloaded function,  I thought he understood how to do
the alternative approach, i.e. making the parameter list unambiguous).

So ... again, all I'm saying is that it's a judgement call as to which  is best.

Collapse
Posted by Roberto Mello on
Vinod, thanks a lot. I imagined this would be the case, but wasn't sure. Thanks for the nice example.

Don, I understood you. I agree with you. Thanks a lot too.