[clean-list] Default values for record fields
Bas van Dijk
basvandijk at home.nl
Fri Dec 3 21:13:58 MET 2004
Peter,
For the course: "Ontwikkeling van grote Software Systemen (OSS)"
I'm developing a converter from XML Schema to Clean datatypes.
An XML schema describes the structure of an XML document, you can see it as
the datatype of an XML Document.
The first thing I'm trying to do is creating Clean datatypes that represent
the Schema. (I included the .dcl file but it's a fist draft).
(After this I need to develop a parser that parses the Schema XML Document and
constructs a value of :: Schema)
In XML Schema some elements have default values. It would be nice if I could
directly include these default values into my records.
So for example:
---------------------------------
:: FormValues = Qualified | Unqualified
:: Schema = {
...
elementFormDefault :: FormValues = Unqualified,
attributeFormDefault :: FormValues = Qualified,
... }
---------------------------------
Now when I want to construct a Schema I don't need to give a value for all
fields just the ones I like.
The use of generic functions will not work in this case because I need to
specify default values for specific fields.
BTW A lot of programming languages allow you to define default values.
For example in python you can say something like this:
---------------------------------
class Student:
name = "Peter Achten"
s = Student
print s.name
Peter Achten
---------------------------------
Bas.
On Friday 03 December 2004 16:51, Peter Achten wrote:
> 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
-------------- next part --------------
A non-text attachment was scrubbed...
Name: XmlSchema.dcl
Type: text/x-java
Size: 7844 bytes
Desc: not available
Url : http://mailman.science.ru.nl/pipermail/clean-list/attachments/20041203/dbe910d0/XmlSchema-0001.bin
More information about the clean-list
mailing list