[clean-list] Default values for record fields
Peter Achten
P.Achten at cs.ru.nl
Fri Dec 3 16:51:45 MET 2004
Dear Bas,
At 12:13 PM 12/3/2004, you wrote:
>Hi,
>
>Is it possible to define default values for record fields?
>
>If it's possible: how?
>If it isn't: why wouldn't this be a good feature?
>
>Bas.
If you don't mind using an experimental feature of Clean, viz. generics,
you can define the following generic function (create new project and set
environment to Everything):
generic gNew a :: a
gNew{|Int|} = 0
gNew{|Bool|} = False
gNew{|Real|} = 0.0
gNew{|String|} = ""
gNew{|Char|} = '\0'
gNew{|UNIT|} = UNIT
gNew{|PAIR|} newA newB = PAIR newA newB
gNew{|EITHER|} newA newB = RIGHT newB
gNew{|CONS|} newA = CONS newA
gNew{|OBJECT|} newA = OBJECT newA
gNew{|FIELD|} newA = FIELD newA
It defines default values for the basic types Int, Bool, Real, String and
Char. It also defines default values for any type that you can come up with
that is a record, algebraic type or arbitrary composition of these types.
It does not work for function types.
E.g., define:
:: MyR = { name::String,age::Int,married::Bool }
derive gNew MyR
Start :: MyR
Start = gNew{|*|}
This lets Start create a default value for MyR: (MyR "" 0 False).
If you define:
:: MyT :== [(Int,MyR)]
derive gNew MyR, (,), []
and change Start's type to:
Start :: MyT
then Start yields: [].
Hope this helps,
Peter
More information about the clean-list
mailing list