PostgreSQL function that returns a 'casted' complex type from 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.