[squeak-dev] [BUG] Issue with Matrix in instance variable

Thiede, Christoph Christoph.Thiede at student.hpi.uni-potsdam.de
Wed Feb 19 09:29:24 UTC 2020


Wait, this is indeed interesting! Why don't we copy contents when passed in the constructor? This really feels like an implementation detail of array.


block := [Matrix rows: 1 columns: 1 contents: #(1)].
x := block value.
x at: 1 at: 1 put: 2.
y := block value.
self assert: [(y at: 1 at: 1) = 1]. "failed, is 2!"

Whereas it works if you define block as:

"block := [Matrix rows: 1 columns: 1 contents: {1}]."

Best,
Christoph

________________________________
Von: Squeak-dev <squeak-dev-bounces at lists.squeakfoundation.org> im Auftrag von Taeumel, Marcel
Gesendet: Mittwoch, 19. Februar 2020 10:08:34
An: John Pfersich via Squeak-dev
Betreff: Re: [squeak-dev] [BUG] Issue with Matrix in instance variable

Hi Karl.

The issue is that, in your example, you are always using the same instance of Array:

initialize
super initialize.
grid := Matrix rows: 3 columns: 3 contents: #(
5 3 0
6 0 0
0 9 8 )

Those "contents" will always be the same for each new instance of TesterClass. So, even though the source code reads "#( 5 3 0 ..." is has actually changed to "#( 9 9 9 ...". Try inspecting "TesterClass >> #initalize" and take a look at #literal5:

[cid:31020277-e79b-4bb0-ba0a-f1540fb09ff2]

This is not a bug. :-) Just a (maybe surprising) side effect of literal arrays.

Best,
Marcel

Am 19.02.2020 09:09:03 schrieb karl ramberg <karlramberg at gmail.com>:

Hi,
I have this simple class TesterClass new.
Doit  in Workspace: TesterClass inspect.
The grid instance variable is initialized according to initialize method.
I doit in inspector: self fillGrid.
The grid matrix is now filled with all 9.
As expected.

Now doit in Workspace: TesterClass new inspect
The grid is all 9.

I think the issue is in
Matrix class>>rows: columns: contents:
rows: rows columns: columns contents: contents
^super new rows: rows columns: columns contents: contents

Matrix class overrides new so I think there is a conflict

Best,
Karl





'From Squeak5.3alpha of 23 October 2019 [latest update: #19139] on 18 February 2020 at 9:22:56 pm'!
Object subclass: #TesterClass
instanceVariableNames: 'grid'
classVariableNames: ''
poolDictionaries: ''
category: 'MyTestStuff'!

!TesterClass methodsFor: 'as yet unclassified' stamp: 'kfr 2/18/2020 21:21'!
fillGrid
grid atAllPut: 9! !

!TesterClass methodsFor: 'as yet unclassified' stamp: 'kfr 2/18/2020 21:18'!
initialize
super initialize.
grid := Matrix rows: 3 columns: 3 contents: #(
5 3 0
6 0 0
0 9 8 )! !
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.squeakfoundation.org/pipermail/squeak-dev/attachments/20200219/a7626153/attachment.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: image.png
Type: image/png
Size: 12967 bytes
Desc: image.png
URL: <http://lists.squeakfoundation.org/pipermail/squeak-dev/attachments/20200219/a7626153/attachment.png>


More information about the Squeak-dev mailing list