Skip to content

Convert data properties to private by default #67

Description

@mlhaufe

Data properties should not be publicly accessible by default. A trait must be defined to expose the desired properties.

This will enable Brevity to support the Uniform access principle. Additionally this will enable better encapsulation and Representation Independence.

const PointData = data({
    Point2: { x: Number, y: Number },
    Polar2: { rho: Number, theta: Number }
});

const PrintTrait = trait('print', {
    Point2({ x, y }) { return `Point2(${x}, ${y})` },
    Polar2({rho, theta}) { return `Polar2(${rho}, ${theta})` }
})

const XTrait = trait('x', {
    Point2({x}) { return x },
    Polar2({rho, theta}){ return rho * Math.cos(theta) }
})

const YTrait = trait('y', {
    Point2({y}) { return y },
    Polar2({rho, theta}) { return rho * Math.sin(theta) }
})

const Point = complect(PointData, [XTrait, YTrait, PrintTrait])

const {Point2, Polar2} = Point()

Point2(12, 5).x()

Polar2(5, 167).x()

Note this will subsume the derived property field of data:

Before:

const EmployeeData = data({
    Employee: {
        firstName: String,
        lastName: String,
        fullName: {
            guard: String, // Optional
            get() { return `${this.firstName} ${this.lastName}` }
        }
    }
})

const {Employee} = complect(EmployeeData)()

const johnDoe = Employee('John', 'Doe')

johnDoe.fullName === 'John Doe'

After:

const EmployeeData = data({
    Employee: { firstName: String, lastName: String }
})

const FullNameTrait = trait('fullName', {
    Employee({firstName, lastName}) { 
        return `${this.firstName} ${this.lastName}`
    }
})

const {Employee} = complect(EmployeeData, [FullNameTrait])()

const johnDoe = Employee('John', 'Doe')

johnDoe.fullName === 'John Doe'

Destructuring

Currently destructuring is allowed on complected instances intuitively as follows:

const disk = Disk({ position: [0, 0], velocity: [1, 3], radius: 1, item: 'apple' });

const [position, velocity, radius, item] = disk;

const { position, velocity, radius, item } = disk;

With the suggested change these would become function references, which seems to imply that they might need to be bound to the instance, otherwise an alternative form may be desirable.

Metadata

Metadata

Assignees

Labels

enhancementNew feature or request

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions