Forums

Shapes at http://onestepback.org/articles/poly/

General discussion about Cobra. Releases and general news will also be posted here.
Feel free to ask questions or just say "Hello".

Shapes at http://onestepback.org/articles/poly/

Postby Csaba » Thu Jan 08, 2009 11:06 am

Hi,
Someone here suggested to write a Cobra version of the program at http://onestepback.org/articles/poly/. The program has not yet appeared and I can’t find the link here so I have started a new thread about this subject.

The problem is that the print command is actually a println. How do I do a print without ‘\n’?

I also append the code I written so far.

I have used Cobra 0.8 since I could not download the recent version correctly
(so var + pro are combined in a single staemets).

It is also possibble to a second Cobra version with "all extra included".

Regards
Csaba

Code: Select all
"""
Shapes.cobra demo for http://onestepback.org/articles/poly/ by CsabaU@Hotmail.com
Minimal version (but the code can be even tighter!)
"""
class Shape
    var _x as float
    var _y as float

    pro x from var # defines a property -`from var` is a shortcut syntax to cover the variable _x.
    pro y from var
   
   def init(initX as float, initY as float)
      _x = initX
      _y = initY
   
   def moveTo(newX as float, newY as float)
      _x = newX
      _y = newY

    def relativeMove(deltaX as float, deltaY as float)
      _x +=deltaX
      _y +=deltaY

def draw
        print 'Draw [.getType.name] at:(  [_x] , [_y] ). ' # with meta info

class Rectangle
    inherits Shape
   
    var _width as float
    var _height as float

    pro width from var
    pro height from var

    def init(initX as float, initY as float, initWidth as float, initHeight as float)
      base.init(initX, initY)
        _width = initWidth
        _height = initHeight
       
    def draw
        base.draw
        print ' With height: [_height], width: [_width]'

class Circle
    inherits Shape
   
    var _radius as float
    pro radius from var

    def init(initX as float, initY as float, initRadius as float )
      base.init(initX, initY)
        _radius = initRadius
       
    def draw
        base.draw
        print ' With radius: [_radius]'
       
class Program

   def main is shared
        shapes  = [Rectangle(10f, 20f, 5f, 6f), Circle(15f, 25f, 8f)] # a list with hard coded shapes
       
        # using shapes polymorphically
        for aShape in shapes
            aShape.draw
            aShape.relativeMove(100f, 100f)
            aShape.draw

        # access a rectangle specific function
        aRectangle= Rectangle(0f, 0f, 15f, 15f)
        aRectangle.width = 30f
        aRectangle.draw
Csaba
 
Posts: 42

Re: Shapes at http://onestepback.org/articles/poly/

Postby Charles » Thu Jan 08, 2009 12:47 pm

Thanks. To stop print from printing a new line, put the keyword stop at the end:
print 'foo = [foo]' stop


Sorry you couldn't get the latest Cobra out of Subversion. There is some nice stuff there. But we're waiting to release 0.9 until these items are done: http://cobra-language.com/trac/cobra/mi ... obra%200.9

I don't think you need the "f" suffix for your floats given that they are whole numbers like "15".

Let's wait until Cobra 0.9 to submit an official version to the site.
Charles
 
Posts: 2515
Location: Los Angeles, CA

Re: Shapes at http://onestepback.org/articles/poly/

Postby Csaba » Thu Jan 08, 2009 2:22 pm

OK, I will wait.
Comparing the impementation of this simple program in different languages is more informative than I would have guessed.
IMO, the Cobra version has the most "obvious" code (except for the 'stop' to supress line feed).

Regards
Csaba
Csaba
 
Posts: 42

Re: Shapes at http://onestepback.org/articles/poly/

Postby Charles » Sun Jan 11, 2009 8:18 pm

I agree. I looked at the other versions and was amazed at how crufty some of their syntax is.

I combed over your implementation to reflect the latest Cobra and to use the coding style commonly seen in other samples, how-to's and the compiler. I then added under Samples\ in the repository. You can see it online here:

cobra/trunk/Samples/Shapes.cobra?rev=1896

cobra/trunk/Samples/Shapes.cobra

The "is abstract" and "inherits" clauses will eventually go on the same line as what they modify, but Cobra doesn't support that yet.
Charles
 
Posts: 2515
Location: Los Angeles, CA


Return to Discussion

Who is online

Users browsing this forum: No registered users and 80 guests