Function sugnature with default parameter - Mailing list pgsql-hackers

From salah jubeh
Subject Function sugnature with default parameter
Date
Msg-id 1393438522.77145.YahooMailNeo@web164803.mail.gq1.yahoo.com
Whole thread Raw
Responses Re: Function sugnature with default parameter  (David Johnston <polobo@yahoo.com>)
List pgsql-hackers
Hello,

I find default values confusing when a function is overloaded, below is an example.

CREATE OR REPLACE FUNCTION default_test (a INT DEFAULT 1, b INT DEFAULT 1, C INT DEFAULT 1) RETURNS INT AS
$$
    BEGIN
        RETURN a+b+c;
    END;
$$
LANGUAGE 'plpgsql';

CREATE OR REPLACE FUNCTION default_test (a INT DEFAULT 1, b INT DEFAULT 1) RETURNS INT AS
$$
    BEGIN
        RETURN a+b;
    END;
$$
LANGUAGE 'plpgsql';

-- this will fail
--SELECT default_test(1,3);
--SELECT default_test(1);

test=# \df default_test
                                                 List of functions
 Schema |     Name     | Result data type |                      Argument data types                      |  Type 
--------+--------------+------------------+---------------------------------------------------------------+--------
 public | default_test | integer          | a integer DEFAULT 1, b integer DEFAULT 1                      | normal
 public | default_test | integer          | a integer DEFAULT 1, b integer DEFAULT 1, c integer DEFAULT 1 | normal
(2 rows)

I think, there is a difference between optional parameters and default parameter values. So, my suggestion would be something like this.

SELECT default_test(1,3, DEFAULT); -- match function number 1
SELECT default_test(1,3); -- match the function number 2
SELECT default_test(1); -- ERROR

Regards

pgsql-hackers by date:

Previous
From: Josh Berkus
Date:
Subject: Re: jsonb and nested hstore
Next
From: Alvaro Herrera
Date:
Subject: Re: Changeset Extraction v7.7