I am writing classes in Pharo Smalltalk, but I assume the question is valid for other Smalltalk implementations.
I know a way to enforce instances with specific attributes is to provide a class method for instance creation, and then suggesting to use the class creation method. But any user know that new or basicNew can be used anytime.
I thought about invalidating new and basicNew raising an exception, but this seems to be too drastic measure, giving that sometimes I could require to create instances to debug for example.
Is there another library or mechanism to enforce those specific attributes to be completed?
There is not. And this is good.
Here are some approaches you might follow though:
Make sure your objects are valid by providing validations for them. This is a very broad topic so I will just say that a validation framework is one able to examine your objects and somehow make it explicit any rules they fail to honor.
Depending on what you are doing, you might restrict validations to the GUI, when objects are born or modified. A more sophisticated approach, however, should allow any object to decide whether it is valid or not (in a given context).
Exclude individual setters from the instance-side protocol. Provide only multiple keyword setters that will fail if something is missing or inappropriate. This means that you would provide methods like
Point >> #x:y:but notPoit >> #x:orPoint >> #y:.As the example suggests, you will find it hard to take this practice at its full extent because basic classes don't follow this style. Also note that this practice will require some kind of validation because checking only for
notNilis usually too naïve.Relax and do nothing until the need arrives for addressing these kinds of issues.
In other words pospone the problem until your software is evolved enough as to call your attention to the way its objects are created and change.
The reason I claim things are good the way they are is because Smalltalk, having traditionally favored openness over security, has facilitated and even encouraged people to experiment, even in the "wrong" way. Every feature aimed at preventing objects from being broken, will sooner or later stop you from repairing them. More importantly, they will consume a lot of energy that could otherwise be applied to more productive ends.