[itn book] colon prefixed mode not working

Started by conan, December 15, 2011, 02:20:18 AM

Previous topic - Next topic

conan

I'm reading the chapter on contexts now on the Introduction to Newlisp book, and just copy pasted the example on FOOP, which looks like this:



; definitions

(define (Time:Time (t (date-value)) (zone 0))
(list Time t zone))

(define (Time:show t)
(date (t 1) (t 2)))

(define (Time:days-between t1 t2)
"Return difference in days between two times."
(div (abs (- (t1 1) (t2 1))) (* 24 60 60)))

(define (Time:get-hours t)
"Return hours."
(int (date (t 1) (t 2) {%H})))

(define (Time:get-day t)
"Return day of week."
(date (t 1) (t 2) {%A}))

(define (Time:leap-year? t)
(let ((year (int (date (t 1) (t 2) {%Y}))))
(and (= 0 (% year 4))
(or (!= 0 (% year 100)) (= 0 (% year 400))))))

; use

(set 'time-now (Time))
(set 'my-birthday (Time (date-value 2008 5 26)))
(set 'christmas-day (Time (date-value 2008 12 25)))

; call functions with full context prefix
(println (Time:show christmas-day))

; or call them with colon prefix mode
(println (:show christmas-day))


Last call gives me this error:



$ newlisp foop.lsp
Wed Dec 24 21:00:00 2008

ERR: invalid function in function date : (MAIN:t 1)
called from user defined function Time:show


I tried switching to the context, just to check cause there will be no fun if you have to do that, but I wanted to check it anyway:



> (context Time)
Time
Time> (set 'christmas-day (Time (date-value 2008 12 25)))
(Time 1230163200 0)
Time> (:show christmas-day)

ERR: invalid function in function date : (MAIN:t 1)
called from user defined function Time:show


What I understood about colon prefix mode was that it decided which function to use based on the class found in position zero. So I though maybe this version was messing with the contexts and tried also this definition for 'show' function:



(define (Time:show Time:t)
    (date (Time:t 1) (Time:t 2)))


Which gives almost the same error:



$ newlisp foop.lsp
Wed Dec 24 21:00:00 2008

ERR: invalid function in function date : (t 1)
called from user defined function Time:show


'MAIN' disappeared, but we still got the same error. So it's like 'show', when called with the colon prefix mode, it's not receiving a list inside 't'.



I don't know if this is something that has changed since that part of the book was writing or if I'm missing something. Any thoughts are welcome.

m i c h a e l

#1
Hi conan!



I'm a little rusty at this, but I ran the code through the debug function and found that t is nil within Time:show (hence the error message). Why? I have no idea! Lutz?



m i c h a e l

Lutz

#2
FOOP has changed with newLISP version 10.2 (beginning of 2010). The WikiBooks Introduction seems to be based on an older version.



Here is a newer FOOP example:



http://www.newlisp.org/complex.cgi">http://www.newlisp.org/complex.cgi



linked from this page:



http://www.newlisp.org/index.cgi?Tips_and_Tricks">http://www.newlisp.org/index.cgi?Tips_and_Tricks



There is also a short introduction in the Users Manual:



http://www.newlisp.org/downloads/newlisp_manual.html#foop">http://www.newlisp.org/downloads/newlis ... .html#foop">http://www.newlisp.org/downloads/newlisp_manual.html#foop

m i c h a e l

#3
I completely forgot about not needing to define a parameter for the object! I said I was rusty ;-)



conan, this means wherever we had a parameter for the object we would remove that  and use self instead:


; definitions

(define (Time:Time (t (date-value)) (zone 0))
   (list Time t zone))

(define (Time:show)
   (date (self 1) (self 2)))

(define (Time:days-between other)
   "Return difference in days between two times."
   (div (abs (- (self 1) (other 1))) (* 24 60 60)))

(define (Time:get-hours)
   "Return hours."
   (int (date (self 1) (self 2) {%H})))

(define (Time:get-day)
   "Return day of week."
   (date (self 1) (self 2) {%A}))

(define (Time:leap-year?)
   (let ((year (int (date (self 1) (self 2) {%Y}))))
      (and (= 0 (% year 4))
         (or (!= 0 (% year 100)) (= 0 (% year 400))))))


m i c h a e l

conan

#4
Thanks Michael and Lutz, I think Cormullion said somewhere in the book to read the reference manual first, not sure though, and now I'm too engaged with the book to start from scratch with the manual. Anyway I should read it when I find this kind of differences, I'm making a mental note now.



A question on style now, regarding the example under the subtitle "Structuring a larger FOOP program" in the newlisp manual, shouldn't class declaration go inside each class defining file?



We got in main:



(new Class 'Rectangle)
(new Class 'Circle)


And I wonder if shouldn't be better to have each of those inside 'Rectangle.lsp' and 'Circle.lsp' respectively.



BTW, I'll be updating itn book in a couple of minutes.



Update



I knew I was forgetting something: the old way to call functions using full name is forbidden now, right?



(SomeClass:some-function) ; doesn't work on newlisp version >= v10.2

conan

#5
itn book updated now. I modified examples and part of the text under subtitles: FOOP in a nutshell, Polymorphism, Modifying objects.



Also I added an external link to the reference manual since there's an example there on nested objects which I felt was important and is missing on itn book. Maybe we should add another section to show that or maybe just leave the link.



Feel free to review this newbie's changes, and if you do, make sure I didn't mess it all.

cormullion

#6
Hey, conan, thanks again for reading the Intro so carefully and for making the time to make changes and comments too. For me, the great thing about wikibooks is that the result can be better than the efforts of any one individual can manage. Feedback from people new to the language is even more useful, sometimes, than comments from people who have settled into their usage patterns.



Indeed the original text for that section looks like it's from 2009, and probably needs updating to match the changes in FOOP that were happening around then.



The Introduction was always written with due deference to the Reference Manual. When in doubt, seek it out...



BTW - there is, in fact, a more recent version of the time-utilities code on my github page: I use it quite often, so it does work better with the current newLISP than the introduction's code.



michael - hello again! How are you‽

m i c h a e l

#7
Quote from: "conan"... shouldn't class declaration go inside each class defining file?


You should put it into its own file if the class definition is large enough or if you will be using the class in multiple projects. Short class definitions or project specific classes don't need to go into separate files.


Quote from: "cormullion"michael - hello again! How are you‽


Hi cormullion! I'm doing great, thanks for asking. Melissa and I started a freelance business after her position was cut at the university, and it has been nonstop ever since. I haven't been doing any programming lately, but I do check this forum every day. How have you been?



m i c h a e l