Function sugnature with default parameter

From: salah jubeh <s_jubeh(at)yahoo(dot)com>
To: "pgsql-hackers(at)postgresql(dot)org" <pgsql-hackers(at)postgresql(dot)org>
Subject: Function sugnature with default parameter
Date: 2014-02-26 18:15:22
Message-ID: 1393438522.77145.YahooMailNeo@web164803.mail.gq1.yahoo.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: 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

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Alvaro Herrera 2014-02-26 18:30:55 Re: Changeset Extraction v7.7
Previous Message Hannu Krosing 2014-02-26 18:08:32 Re: jsonb and nested hstore