Re: map anomaly

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

Let me demonstrate how I use maps and it may help you understand what you can do with them.

I generally use maps to create "class-like" objects in Euphoria. Here is a quick example.

C++ code (see: Simple_class_example)

// simple_class_example.cpp 
class Person { 
public: 
      Person(string name, int age) { 
            this->name = name; 
            this->age = age; 
      } 
      string getName() { 
            return name; 
      } 
      int getAge() { 
            return age; 
      } 
private: 
      string name; 
      int age; 
}; 
 
int main() { 
      cout << "Creating a person..." << endl; 
 
      Person johnDoe("John Doe", 25); 
      cout << "Person's name: " << johnDoe.getName() << endl; 
      cout << "Person's age: " << johnDoe.getAge() << endl; 
 
      return 0; 
} 

Euphoria code

-- person.e 
namespace person 
 
include std/map.e 
 
public type person( object p ) 
 
    -- make sure this is at least a map 
    if not map(p) then 
        return 0 
    end if 
 
    -- verify this is a person object by checking 
    -- that some or all of the properties exist 
    return map:has(p, "name") and map:has(p, "age") 
end type 
 
public function new( sequence name, integer age ) 
 
    -- create a small map to hold just a few values 
    map p = map:new( threshold() ) 
 
    -- store some values in the 'object' 
    map:put( p, "name", name ) 
    map:put( p, "age", age ) 
 
    return p 
end function 
 
public function get_name( person p ) 
    sequence name = map:get( p, "name" ) 
    return name 
end function 
 
public function get_age( person p ) 
    integer age = map:get( p, "age" ) 
    return age 
end function 

Testing...

include person.e 
 
procedure main() 
 
    puts( 1, "Creating a person...\n" ) 
 
    person johnDoe = person:new( "John Doe", 25 ) 
 
    if person( johnDoe ) then 
        printf( 1, "Person's name: %s\n", {person:get_name(johnDoe)} ) 
        printf( 1, "Person's age: %d\n", {person:get_age(johnDoe)} ) 
    end if 
 
    atom steveSmith = 5 
 
    if not person( steveSmith ) then 
        printf( 1, "steveSmith is not a person!\n" ) 
    end if 
 
end procedure 

-Greg

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

Search



Quick Links

User menu

Not signed in.

Misc Menu