OOP with Kawa

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

OOP with Kawa

Jérôme Brilland
Hello,

I am starting object oriented programming with Kawa :

(define (printf v . args)
     (apply format #t v args))

(define-simple-class <building>()
     (x type: int)
     (y type: int)
     (name type: string)
     )

(define f
     (<building>
         x: 5
         y: 7
         name: "farm"))

(define (displayln v)
     (display v)
     (newline))
(displayln "creating class...")

(printf "~A : (~D, ~D)" f:name f:x f:y)(newline)
(set! f:x 6)
(printf "~A : (~D, ~D)" f:name f:x f:y)(newline)


I get the following warnings :
test3.scm:22:25: warning - no known slot 'name' in java.lang.Object
test3.scm:22:32: warning - no known slot 'x' in java.lang.Object
test3.scm:22:36: warning - no known slot 'y' in java.lang.Object

The Kawa documentation lacks examples.
Could someone provide me with OOP examples ?

Thanks,
Jérôme


Reply | Threaded
Open this post in threaded view
|

Re: OOP with Kawa

Per Bothner


On 12/01/2014 12:13 PM, Jérôme Brilland wrote:
> I get the following warnings :
> test3.scm:22:25: warning - no known slot 'name' in java.lang.Object
> test3.scm:22:32: warning - no known slot 'x' in java.lang.Object
> test3.scm:22:36: warning - no known slot 'y' in java.lang.Object
>
> The Kawa documentation lacks examples.

True.  It also needs a tutorial.

> Could someone provide me with OOP examples ?

Actually, your example is fine.  If you put it in a file ob.scm,
then run Kawa is whole-file-mode, you get no warnings:

$ kawa /tmp/ob.scm
creating class...
farm : (5, 7)
farm : (6, 7)

If you run in line-at-a-time module you get warnings,
but it still works:

$ kawa -f /tmp/ob.scm
creating class...
/tmp/ob.scm:21:25: warning - no known slot 'name' in java.lang.Object
/tmp/ob.scm:21:32: warning - no known slot 'x' in java.lang.Object
/tmp/ob.scm:21:36: warning - no known slot 'y' in java.lang.Object
farm : (5, 7)
/tmp/ob.scm:23:25: warning - no known slot 'name' in java.lang.Object
/tmp/ob.scm:23:32: warning - no known slot 'x' in java.lang.Object
/tmp/ob.scm:23:36: warning - no known slot 'y' in java.lang.Object
farm : (6, 7)

To shut up the warnings, Kawa needs to be able to deduce that f
is a <building> - and can't be changed to something else.  So either
use define-constant:

(define-constant f
     (<building>
         x: 5
         y: 7
         name: "farm"))

or add a type specifier:

(define f :: <building>
     (<building>
         x: 5
         y: 7
         name: "farm"))

or just run in whole-file (module) mode.
--
        --Per Bothner
[hidden email]   http://per.bothner.com/
Reply | Threaded
Open this post in threaded view
|

Re: OOP with Kawa

Jérôme Brilland
Hello,

On 01/12/2014 21:22, Per Bothner wrote:

> To shut up the warnings, Kawa needs to be able to deduce that f
> is a <building> - and can't be changed to something else. So either
> use define-constant:
>
> (define-constant f
>     (<building>
>         x: 5
>         y: 7
>         name: "farm"))
>
> or add a type specifier:
>
> (define f :: <building>
>     (<building>
>         x: 5
>         y: 7
>         name: "farm"))
>
> or just run in whole-file (module) mode.
Thanks !

Now I am trying the define-class syntax, but I get errors :

(define-class <moveable>()
     (x type: int)
     (y type: int)
     ((move ax ay)
         (displayln "moving object...")
         (set! x ax)
         (set! y ay)
         )
     )

(define-class <unit> (<moveable>)
     (strength type: int)
     (name type: string)
     )

(define u :: <unit>
     (<unit>
         x: 6
         y: 17
         name: "warrior"
         strength: 100
         )
     )

(printf "~A : (~D, ~D) : ~D" u:name u:x u:y u:strength)(newline)
(u:move 11 12)
(printf "~A : (~D, ~D): ~D" u:name u:x u:y u:strength)(newline)

But I get the following errors :

test4.scm:40:30: caught exception in inliner for #<procedure
gnu.kawa.functions.GetNamedPart> - java.lang.NoClassDefFoundError:
atInteractiveLevel$3
     java.lang.Class.getDeclaredClasses0(Native Method)
     java.lang.Class.getDeclaredClasses(Class.java:1766)
     java.lang.Class$2.run(Class.java:1354)
     java.security.AccessController.doPrivileged(Native Method)
     java.lang.Class.getClasses(Class.java:1348)
     gnu.bytecode.ClassType.addMemberClasses(ClassType.java:241)
     gnu.bytecode.ClassType.getDeclaredClass(ClassType.java:166)
     gnu.kawa.reflect.SlotGet.lookupMember(SlotGet.java:229)

I really would like a few examples...

Jérôme

Reply | Threaded
Open this post in threaded view
|

Re: OOP with Kawa

Per Bothner


On 12/02/2014 02:31 PM, Jérôme Brilland wrote:

> Now I am trying the define-class syntax, but I get errors :
>
> (define-class <moveable>()
>      (x type: int)
>      (y type: int)
>      ((move ax ay)
>          (displayln "moving object...")
>          (set! x ax)
>          (set! y ay)
>          )
>      )
>
> (define-class <unit> (<moveable>)
>      (strength type: int)
>      (name type: string)
>      )
>
> (define u :: <unit>
>      (<unit>
>          x: 6
>          y: 17
>          name: "warrior"
>          strength: 100
>          )
>      )
>
> (printf "~A : (~D, ~D) : ~D" u:name u:x u:y u:strength)(newline)
> (u:move 11 12)
> (printf "~A : (~D, ~D): ~D" u:name u:x u:y u:strength)(newline)

Please always submit a complete self-contained test case if at all possible.
Use an attachment if there is any chance formatting might get screwed up.
Also include instructions - .e.g. what commands you used.

Specifically, I had to replace (printf ...) by (format #t ...)
and displayln by display.

> But I get the following errors :
>
> test4.scm:40:30: caught exception in inliner for #<procedure gnu.kawa.functions.GetNamedPart> - java.lang.NoClassDefFoundError: atInteractiveLevel$3
>      java.lang.Class.getDeclaredClasses0(Native Method)
>      java.lang.Class.getDeclaredClasses(Class.java:1766)
>      java.lang.Class$2.run(Class.java:1354)
>      java.security.AccessController.doPrivileged(Native Method)
>      java.lang.Class.getClasses(Class.java:1348)
>      gnu.bytecode.ClassType.addMemberClasses(ClassType.java:241)
>      gnu.bytecode.ClassType.getDeclaredClass(ClassType.java:166)
>      gnu.kawa.reflect.SlotGet.lookupMember(SlotGet.java:229)

I don't see this.  Is it possible you're using an old version?
It's usually best to build from source checked out from svn.
Or wait for 2.0, which I intend to release this evening ...

When I run *without* the -f option then I see warnings:

/tmp/bar.scm:17:5: warning - no field or setter 'x' in class bar$unit$class
/tmp/bar.scm:17:5: warning - no field or setter 'y' in class bar$unit$class

I think this is a bug because Kawa doesn't handle not-yet-compiled classes
in this case.  That needs to be fixed ...

> I really would like a few examples...

There are some in the Kawa source code.  If you have the Unix-style tools
available, look for them with grep+find:

$ grep define-simple-class `find . -name '*.scm'`
$ grep define-class `find . -name '*.scm'`

I do hope to spend some time on the Tutorial fairly soon,
and I'll try to come up with step-by-step simple examples.

--
        --Per Bothner
[hidden email]   http://per.bothner.com/
Reply | Threaded
Open this post in threaded view
|

Re: OOP with Kawa

Alcides Flores Pineda
Hello Jérôme:

I wrote this snippets while I was teaching myself to write/use some
classes in Kawa:

https://gist.github.com/alcidesfp/3682358
https://gist.github.com/alcidesfp/2695774
https://gist.github.com/alcidesfp/2695774
https://gist.github.com/alcidesfp/2492398

I dont know if they may help you or hopefully they could provide some
basic ideas for the documentation/tutorial(s).

Greetings.
--
Alcides Flores Pineda

On Tue, Dec 02 2014, Per Bothner wrote:

> On 12/02/2014 02:31 PM, Jérôme Brilland wrote:
>> Now I am trying the define-class syntax, but I get errors :
>>
>> (define-class <moveable>()
>>      (x type: int)
>>      (y type: int)
>>      ((move ax ay)
>>          (displayln "moving object...")
>>          (set! x ax)
>>          (set! y ay)
>>          )
>>      )
>>
>> (define-class <unit> (<moveable>)
>>      (strength type: int)
>>      (name type: string)
>>      )
>>
>> (define u :: <unit>
>>      (<unit>
>>          x: 6
>>          y: 17
>>          name: "warrior"
>>          strength: 100
>>          )
>>      )
>>
>> (printf "~A : (~D, ~D) : ~D" u:name u:x u:y u:strength)(newline)
>> (u:move 11 12)
>> (printf "~A : (~D, ~D): ~D" u:name u:x u:y u:strength)(newline)
>
> Please always submit a complete self-contained test case if at all possible.
> Use an attachment if there is any chance formatting might get screwed up.
> Also include instructions - .e.g. what commands you used.
>
> Specifically, I had to replace (printf ...) by (format #t ...)
> and displayln by display.
>
>> But I get the following errors :
>>
>> test4.scm:40:30: caught exception in inliner for #<procedure
>> gnu.kawa.functions.GetNamedPart> - java.lang.NoClassDefFoundError:
>> atInteractiveLevel$3
>>      java.lang.Class.getDeclaredClasses0(Native Method)
>>      java.lang.Class.getDeclaredClasses(Class.java:1766)
>>      java.lang.Class$2.run(Class.java:1354)
>>      java.security.AccessController.doPrivileged(Native Method)
>>      java.lang.Class.getClasses(Class.java:1348)
>>      gnu.bytecode.ClassType.addMemberClasses(ClassType.java:241)
>>      gnu.bytecode.ClassType.getDeclaredClass(ClassType.java:166)
>>      gnu.kawa.reflect.SlotGet.lookupMember(SlotGet.java:229)
>
> I don't see this.  Is it possible you're using an old version?
> It's usually best to build from source checked out from svn.
> Or wait for 2.0, which I intend to release this evening ...
>
> When I run *without* the -f option then I see warnings:
>
> /tmp/bar.scm:17:5: warning - no field or setter 'x' in class bar$unit$class
> /tmp/bar.scm:17:5: warning - no field or setter 'y' in class bar$unit$class
>
> I think this is a bug because Kawa doesn't handle not-yet-compiled classes
> in this case.  That needs to be fixed ...
>
>> I really would like a few examples...
>
> There are some in the Kawa source code.  If you have the Unix-style tools
> available, look for them with grep+find:
>
> $ grep define-simple-class `find . -name '*.scm'`
> $ grep define-class `find . -name '*.scm'`
>
> I do hope to spend some time on the Tutorial fairly soon,
> and I'll try to come up with step-by-step simple examples.