logo
The Falcon Programming Language
A fast, easy and powerful programming language
Location: Home page >> Falcon wiki
User ID:
Password:
 

Falcon Wiki - Manual:Classes1


Work in progress...

Introduction

Well, now we have three modules (pokermachine, poker and decks) where we put some constants and enums. Now the application must get more complicated but first I have to explain what we need and why.

Let's give a look to decks.fal: We have two enums where we have described the values of seeds (ENUSEEDS) and suits (ENUSUITS) and that the best way make our code readable.

Now we have to create, finally, our deck. Logically a deck is a formed by a certain number (defined by constant cardsInDeck) of entities named cards, while a card is an entity with two characteristics: a seed and a suit.

Let's concentrate on the entity Card, for now.

As wrote above, we need an "something" which we'll call a card, with two values: seed and suit.

This is the moment to speak about Classes and instances

Classes

The philosophy behind creating entities from an template and work with them is well known in computer science. It's called OOP: Object Oriented Programming.

Our "template" is called Class and its a schema describing the standard characteristic and behaviors of a object. The Characteristic are values describing the object and they are called properties and the behaviors, that is the actions that object can do, are called methods.

Create an entity from a class is called instantiation and the entity is an instance of the class.

The advantages in using classes are many: First of all, working with "objects" is more near to human's thought. working with a card is more easy that to work with a bunch of constants and variables. Another advantage is that all the algorithms (and variables) are included in the object: so you can use the object without knowing all stuff inside the object.

There are lot of books and articles about OOP. I suggest you read one or more of these to deepen on the argument.

Let's write the schema:

class Card( Suit, Seed )
   seed = ENUSEED.UNDEFINED
   suit = ENUSUIT.UNDEFINED

   init
      self.seed = Seed
      self.suit = Suit
   end

end

Let's explain the code. class is the keyword indicating we are creating a class and the two variables (Suit and Seed) will be used during instantiation. Inside the class we find two variables: seed and suit initializated to the correct UNDEFINED label. Note that these variables are not the same to the two above (suit is different from Suit and seed is different from Seed!).

self keyword

In lines 6 and 7 there is a new keyword: self. Its use is to remind to Falcon that the item (we are looking from inside the class!) or the method is a member of the class. Inside a class we have to use self when we use a member of the class.

init block

Immediately below is the init keyword: this the explicit initializer, also known as explicit constructor. init is a block (closed by the inner end) that will be executed only during instantiation.

Let's make an example: you want to instantiate a card, and precisely the King of Hearts. you can instantiate the card and then set the properties to correct values.

card = Card()
card.seed = ENUSEED.Hearts
card.suit = ENUSUIT.King

Using an explicit constructor we can create an instance with a single line of code:

card = Card(ENUSUIT.King, ENUSEED.Hearts)

Static properties

Every instance has a separated life and changing a property in an instance has no effect on the other instances, but there's a way to have informations shared among all instances using static keyword.

this is an example:

class Card( Suit, Seed )
   static numOfCardCreated = nil
   
   seed = ENUSEED.UNDEFINED
   suit = ENUSUIT.UNDEFINED

   init
      self.seed = Seed
      self.suit = Suit
   end

end

Note that this is only a example to explain static properties, don't put it in your modules!

We can access static properties directly from the class (and not from instances), as in the next example:

> Card.numOfCardCreated				// show nil
card1 = Card( ENUSUIT.Jack, ENUSEED.Hearts )	// created an instance of Card, the Jack of hearts
card1.numOfCardCreated = 1			// numOfCardCreated = 1
> Card.numOfCardCreated				// show one
card2 = Card( ENUSUIT.Six, ENUSEED.Hearts )	// created an instance of Card, the six of hearts
++card2.numOfCardCreated			// Increase numOfCardCreated
> Card.numOfCardCreated				// show two
> card2.numOfCardCreated			// show two (it's same property!!)

Static Init blocks

An init block can contain a static block. While the init block is executed everytime a new instance is created, the code inside the static block is executed only when the first instance is created.

For example:

class Card( Suit, Seed )
   static numOfCardCreated = nil
   seed = ENUSEED.UNDEFINED
   suit = ENUSUIT.UNDEFINED

   init
      static
	 > "the first card is created!"
	 numOfCardCreated = 0
      end
      ++numOfCardCreated
      self.seed = Seed
      self.suit = Suit
   end

end

Note that this is only a example to explain static init blocks, don't put it in your modules!

Now we can rewrite the static properties example in this way:

> Card.numOfCardCreated				// show nil
card1 = Card( ENUSUIT.Jack, ENUSEED.Hearts )	// created an instance of Card, the Jack of hearts
> Card.numOfCardCreated				// show one 
card2 = Card( ENUSUIT.Six, ENUSEED.Hearts )	// created an instance of Card, the six of hearts
> Card.numOfCardCreated				// show two
> card2.numOfCardCreated			// show two (it's same property!!)

We don't have to update numOfCardCreated manually, because the init block and the static block worked for us!. That's an OOP advantage (and it's not present in procedural programming).

Card class

Well, now that we are in touch with classes. open the card.fal and put in there our Card class.

/***************************************
File name: card.fal
Create by: {yourNameHere}
Create date: {creationDateHere}
----------------------------------------
This file contains Card Class
***************************************/
class Card( Suit, Seed )
   
   seed = ENUSEED.UNDEFINED
   suit = ENUSUIT.UNDEFINED

   init
      self.seed = Seed
      self.suit = Suit
   end
   
end

export Card

We have already explained all we wrote in this class, so I'm not going to bore you...

Remember to add "load card" statement both to deck.fal and pokermachine.fal.

Conclusions

In this section we introduced you classes, instances, properties and methods. We'll use them exensively in next section! We have a lot more to view about classes but we'll introduce the new arguments while developing our application.

In the next section we'll speak about Data Structures (first Part)

Last update : 2010-01-11 17:39 (GMT-5) by pnema

Work in progress...


Navigation
Go To Page...

Loading

Elapsed time: 0.027 secs. (VM time 0.021 secs.)