pharo icon indicating copy to clipboard operation
pharo copied to clipboard

Better api for beReadOnlyObject

Open Ducasse opened this issue 1 year ago • 0 comments

imagine the method

A >> returnAnArray
   ^ #(1 2 3) beReadonlyObject

A new returnAnArray 
returns true

Why ? Because

beReadOnlyObject
	"If the VM supports read-only objects it will not write to read-only objects.
	 An attempt to write to an instance variable of a read-only object will
	 cause the VM to send attemptToAssign:withIndex: to the read-only object.
	 An attempt to modify a read-only object in a primitive will cause the
	 primitive to fail with a #'no modification' error code.
	 Set the read-only flag of the receiver to true and answer the previous vaue of the flag."
	^self setIsReadOnlyObject: true

To avoid to force the receiver to define

experienceThree

	| a |
	a := #( 1 2 3).
	a beReadOnlyObject.
	^ a

About beReadOnlyObject, is it the previous value of the flag used? We could have two APIs beReadOnlyObject and setIsReadOnlyObject: with beReadOnlyObject defined as

beReadOnlyObject
	"If the VM supports read-only objects it will not write to read-only objects.
	 An attempt to write to an instance variable of a read-only object will
	 cause the VM to send attemptToAssign:withIndex: to the read-only object.
	 An attempt to modify a read-only object in a primitive will cause the
	 primitive to fail with a #'no modification' error code.
	 Set the read-only flag of the receiver to true and answer the previous vaue of the flag."
	self setIsReadOnlyObject: true.
        ^ self 

and if the user needs the old value just uses setIsReadOnlyObject:

Ducasse avatar Jun 24 '24 13:06 Ducasse