Type gripe

new topic     » topic index » view thread      » older message » newer message

As people may have guessed from a rather out-of-the-blue message in the 'the
Almighty Epoch' thread, I'm monitoring the Euphoria world again. Some old
hands around here might recognise the name anyways :)

Anyway, to the subject at hand:

I'm writing a DateTime.e library for handling things like the 1970 epoch in
the aforementioned thread. I had half a library previously (hence replying
to the thread), but I found that there wasn't enough meat to release it as a
proper library.

I try to declare a Date type like so:

type Date(sequence d)
    return length(d) = 3 and isyear(d[1]) and ismonth(d[2]) and isday(d[3])
end type

...but if I do this:

integer i
i = 1
? Date(i) -- is i a valid Date?

...I get an error because 'i' is not a sequence and so doesn't match the
parameter for the type declaration. Fair enough.

I try again:

type Date(object d)
    return sequence(d) and length(d) = 3 and
        isyear(d[1]) and ismonth(d[2]) and isday(d[3])
end type

integer i
i = 2
? Date(i) -- is i a valid Date?

... This time I get an error that I can't do length() on an atom

* This is my first gripe - returns in types should be short circuited like
if statements since they only return booleans...

Trying for a second time:

type Date(object d)
    if sequence(d) and length(d) = 3 and
       isyear(d[1]) and ismonth(d[2]) and isday(d[3]) then
        return 1
    end if
    return 0
end type

integer i
i = 3
? Date(i) -- is i a valid Date? -- prints 0

...finally something that works.

* My second gripe is - why do I have to do so much when the first example I
gave should do this in the first place?

On the plus side there is a rather natty and neat way of laying out type
definitions like the working example above e.g.:

type isday(object d)
    if integer(d) and d >= 1 and d <= 31 then
        return 1
    end if
    return 0
end type

becomes:

type isday(object d) if integer(d) and
    1 <= d and d <= 31
then return 1 end if  return 0  end type

...which (IMHO) is very much like saying (but I don't suggest this as
syntax):

  d is an integer and 1 <= d <= 31

Views anyone?

Boy, it's good to be back, :)
Carl R. White

new topic     » topic index » view thread      » older message » newer message

Search



Quick Links

User menu

Not signed in.

Misc Menu