Logo

Programming-Idioms

This language bar is your friend. Select your favorite languages!

Idiom #90 Read-only outside

Expose a read-only integer x to the outside world while being writable inside a structure or a class Foo.

use Moops;
class Foo {
    lexical_has 'x', isa => Int, accessor => \(my $_x), default => 0;
    method x() { return $self->$_x }
    method increment_x() { $self->$_x(1 + $self->$_x) }
}
package Foos is
      
   type Foo is private;
      
   function X (Self : Foo) return Integer;
      
private
   type Foo is
      record
         X : Integer;
      end record;
      
end Foos;
   
package body Foos is
      
   function X (Self : Foo) return Integer is (Self.X);
      
end Foos;

New implementation...
< >
bbtemp