Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8841053
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T10:36:57+00:00 2026-06-14T10:36:57+00:00

In a small set of classes for modeling chess situations, I wanted to build

  • 0

In a small set of classes for modeling chess situations, I wanted to build a class to represent positions. But I wanted to create (and export) only the valid 64 positions and make the constructor private after this to prevent the system from creating multiple objects for the same position. Basically a singleton, but with 1 = 64. A 64-ton. Kind of.

Possible usage:

use Positions; # exports positions

my $pos = e4;

# something happens...
do_foo() if $pos == d5;
do_bar() if $pos->row == 8;

My solution works the way I want but it feels kind of inelegant and over-designed.

Basics:

package Position;
use Moo;

has column  => (is => 'ro');
has row     => (is => 'ro');
has name    => (is => 'lazy');

sub _build_name {
    my $self = shift;
    return $self->column . $self->row;
}

sub to_string { shift->name }

# ...

There’s no error check for valid columns or rows with isa because I don’t want the class user to instantiate position objects at all. Now to the exporting:

# prepare 64 positions
my %position;
foreach my $col ('a' .. 'h') {
    foreach my $row (1 .. 8) {

        # build
        my $name    = "$col$row";
        my $pos     = Cherl::Position->new(column => $col, row => $row);

        # remember
        $position{$name} = $pos;
    }
}

# export
sub import {
    my $class   = shift;
    my $caller  = caller;

    # magic!
    no strict 'refs';
    for my $name (sort keys %position) {
        *{$caller . '::' . $name} = sub { $position{$name} };
    }
}

# make it private
sub BUILDARGS {
    my ($class, @args) = @_;
    die 'private' unless +(caller 1)[0] eq __PACKAGE__;
    return $class->SUPER::BUILDARGS(@args);
}

The alternative I could think of

would be to add an error check for columns and rows and overload the == operator to return true if two position’s names are equal to compare positions properly. I’m a little bit scared to build a big number of objects for the same position in this scenario, but maybe this is early optimization.

So the question is if there’s a better way to design this class (using Moo). TIA! 🙂

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T10:36:58+00:00Added an answer on June 14, 2026 at 10:36 am

    I would probably have a Create() method (instead of new X…), with code inside Create() so it will only create the legal positions. You could even make it return the current position object for that position if you tried to call Create() again on that position.

    Also, you might even make the actual position class a hidden child class of the Create()-ing class if you are worried about accidentally directly creating position objects.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to create a small set of classes implementing a safe flag pattern,
I have a set of small images which is working fine in Firefox but
for a small science project I set up a Simulation class which holds all
I want to create a small Ruby project with 10 ~ 20 classes/files. I
I am designing a set of small C++ classes which all implement a single
I've been working on a small set of classes in PHP 5.3.x that follow
Here are my classes: public class XDetail { public string Name { get; set;
I've got a very small set of classes built up in a custom package
Given the following classes: class Department { public String Name { get; set; }
Still coming from C++ I find it cumbersome to create many small helper classes

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.