Skip to Main Content

Database Software

Allow default values for object attributes

William RobertsonJan 13 2016 — edited Jan 19 2016

There are quite a few things I'd like to see added to the PL/SQL OO implementation such as encapsulation (which so many people have been requesting for the last decade and a half that I'm not going to bother), but I came up against one restriction this week that should be pretty simple to fix.

In PL/SQL we can define default values for procedure arguments:

procedure someproc

    ( p_someparam integer default 0 )

and in SQL we can set default values for columns (so the SQL language already has a concept of default values):

create table log

    ( log_timestamp timestamp default systimestamp

but we can't do this for a type:

create or replace type test_t as object
    ( some_attribute integer default 0 )

LINE/COL  ERROR
---------- ---------------------------------------------------------------------------------------------------
0/0        PL/SQL: Compilation unit analysis terminated
2/3        PLS-00363: expression 'SOME_ATTRIBUTE' cannot be used as an assignment target

Yet this is possible in other languages. In Swift for example you can do this:

struct Fahrenheit {
    var temperature = 32.0
}

In Scala you can do this:

class HashMap[K,V](initialCapacity:Int = 16, loadFactor:Float = 0.75f) {

}

In Ada 2012 you can set both defaults and valid ranges:

type OK is new Boolean

    with Default_Value => True;

type Location is

    record

      X: My_Float range –1.0 .. +1.0 := 0.0;

      Y: My_Float range  –1.0 .. +1.0;

    end record;

Unless I am missing something (comments please if I am) this would remove the need for a lot of constructor code.

Comments
Post Details
Added on Jan 13 2016
2 comments
3,686 views