PostgreSQL function that returns a 'casted' complex type from query I tried to define a PostgreSQL function that returns a complex type from a query via:

CREATE TYPE last_contract AS (
    contract_id bigint,
    contract_date timestamp
);


and

CREATE OR REPLACE FUNCTION delivery_last_contract(delivery bigint) RETURNS last_contract AS $func$
DECLARE ret last_contract;
BEGIN

    SELECT DISTINCT ON (tdf.delivery_id) ROW(tcf.contract_id, tcf.date)::last_contract INTO ret
        FROM task_delivery_fetch tdf 
        INNER JOIN task_contract_fetch tcf ON tcf.fetch_id = tdf.fetch_id 
        WHERE tdf.delivery_id = delivery
        ORDER BY tdf.delivery_id, tcf.contract_id DESC
        LIMIT 1;


    RETURN ret;

END;
$func$ LANGUAGE plpgsql;


sadly, all I get is an error:

SELECT delivery_last_contract(12197);
ERROR:  invalid input syntax for integer: "(13605,"2016-12-06 00:00:00+01")"
CONTEXT:  PL/pgSQL function delivery_last_contract(bigint) line 5 at SQL statement


Isn't there a way to actually return a single complex type returned from a query?
CREATE OR REPLACE FUNCTION delivery_last_contract(delivery bigint) 
                   RETURNS last_contract AS $func$
  DECLARE ret last_contract;
  BEGIN
    SELECT tcf.contract_id, tcf.date
      INTO ret
      FROM task_delivery_fetch tdf 
      INNER JOIN task_contract_fetch tcf ON tcf.fetch_id = tdf.fetch_id 
     WHERE tdf.delivery_id = delivery
     ORDER BY tdf.delivery_id, tcf.contract_id DESC
     LIMIT 1;
    RETURN ret;    
  END;
$func$ LANGUAGE plpgsql;


works. Simpler is better.