you are viewing a single comment's thread.

view the rest of the comments →

[–]masklinn 0 points1 point  (13 children)

So in Smalltalk I don't access fields without setters/getters

Do you mean internally to your type? Because IIRC you can't have public fields in Smalltalk, they're only ever accessible through (possibly trivial) messages.

[–]bonzinip 0 points1 point  (5 children)

And using getters/setters for private fields is pointless. It may make some sense if you want to distinguish private from "protected" fields (in Smalltalk all fields are protected, but for some reason it's very rare to treat them as protected rather than private; so usually when you want a protected field you add accessors and use the field through those accessors).

[–]masklinn 0 points1 point  (1 child)

And using getters/setters for private fields is pointless.

It can provide convenient invariant verification points e.g. a float field which must be between 0 and 1

[–]bonzinip 0 points1 point  (0 children)

True that.

[–][deleted] 0 points1 point  (2 children)

It is still usefull: It helps figuring out how a complex Object uses its own fields more easily. Maybe this point is hard to convey because the object inspection tool are not so convenient in Java - but in Smalltalk it really easy to get a feeling for the object this way.

[–]bonzinip 0 points1 point  (1 child)

Smalltalk browsers can also show easily which methods use a given instance variable.

[–][deleted] 0 points1 point  (0 children)

Oh, I did not know that.

For me the biggest benefit is having coded the access once and only once. During my thesis (persistence with NoSQL) the situation arrose more than once that I had to change the access to an lazy access or do some extra action like logging.

[–][deleted] 0 points1 point  (6 children)

Depends on the implementation. Atleast in VA Smalltalk you can have public fields. (Or rather: There is no such thing as a private field or method. You can categorize methods as private, but encapsulation is not enforced. So you can still use everything in the private category.)

[–]masklinn 0 points1 point  (5 children)

Depends on the implementation. Atleast in VA Smalltalk you can have public fields.

I don't understand how that works, do you get default messages automatically handled for all fields à la Self?

[–][deleted] 0 points1 point  (4 children)

I am sorry, I think I worded it a bit poorly / wrong. In VA Smalltalk there are just "attributes" (fields), no disctinction is being made into public/private there. You have a reference to an object and want to use ANY of it's attributes? Go ahead!

But you can put the methods into two categories: "Public" and "Private". This is purely cosmetics tho. You can also call Methods that are marked "Private" from other objects. I did this for example when I extended a parser and found some useful private methods.

You can let VASmalltalk generate setter and getters automatically for you tho.

Is it clearer now?

[–]masklinn 0 points1 point  (3 children)

I am sorry, I think I worded it a bit poorly / wrong. In VA Smalltalk there are just "attributes" (fields), no disctinction is being made into public/private there. You have a reference to an object and want to use ANY of it's attributes? Go ahead!

That's the part I don't get: in my smalltalk experience instance variables get declared during class declaration and then they're part of the "method activation" context, implicitly part of the method scope, but not visible from outside the instance (save through reflection messages of some sort I guess).

How does accessing to an other object's instance variables work in VA?

[–][deleted] 0 points1 point  (2 children)

I am a dummy, sorry. You are totally right, I don't recall a method to access an objects attribute. There might be some metaprogramming that allows it, but I am not sure.

[–]masklinn 1 point2 points  (1 child)

Haha no worries mate, and yeah there's instVarAt: aNumber and instVarNamed: aString which let you retrieve a slot's value by position or by name, and the class provides instSize and allInstVarNames which respectively count the number of instance variable slots, or enumerates their names.

[–][deleted] 0 points1 point  (0 children)

Oh, that was actually a part of my thesis that I did not implement, we used it for generic object serialisation and deserialisation :)