[Newbies] Re: FileDirectory
ron at usmedrec.com
Thu May 26 14:05:53 UTC 2016
From: beginners-bounces at lists.squeakfoundation.org [mailto:beginners-bounces at lists.squeakfoundation.org] On Behalf Of Joseph Alotta
Sent: Wednesday, May 25, 2016 7:47 PM
To: beginners at lists.squeakfoundation.org
Subject: [Newbies] Re: FileDirectory
> Excellent. Remember that you should also look at the super classes for methods. UnixFileDirectory is the instance of “FileDirectory default” but it’s also a subclass of FileDirectory so the methods you can call include UnixFileDirectory and all the methods on the class hierarchy including the methods on FileDirectory. Please see the method categories on FileDirectory that I mentioned.
Okay, thanks. I tried to do something with that, but I got an error. I think I was trying to use a class method on the instance.
> The question I asked was a bit different. When you ask for the #entries of the instance “FileDirectory default” what class is each file?
It is an Array of DirectoryEntry objects.
> By the way, based on the question you asked there is a method on FileDirectory to determine if the file exists before you read it (it’s in the method categories I pointed out to you). What is that method?
I see what is happening. I thought the on: method changes the path, like cding a directory. But it gives a new instance.
dir := FileDirectory default.
dir on: '/Users/jja/Desktop’ ==> UnixFileDirectory on '/Users/jja/Desktop'
dir fileExists: 'categories.CSV’ ==>false
dir pathName ==>'/Users/jja/Desktop/Squeak-4.3-All-in-One.app/Contents/Resources'
dir := FileDirectory on: '/Users/jja/Desktop’ ==> UnixFileDirectory on '/Users/jja/Desktop'
dir pathName ==> '/Users/jja/Desktop'
dir fileExists: 'categories.CSV’ ==> true
So on: is the equivalent of new, in that it gives a new instance.
Exactly right. Notice the on: method on the class side, then look at #on: on the instance side. This is one of those “ah ha” moments. Classes create instances so when you see something like: “Class on:” you should automatically notice that the method is being sent to the class side since you are sending the message to the class directly. This can get confusing if there is a method that results in something like myClass := Class, or when a method calls “self class on:”. But again when it’s done the variable holds the class itself and you are still sending messages to a class when you use “myClass on:”. So when you are talking directly to a class the method is on the class side. Class side method (unless they are like the method above returning a class) usually return instances of the class, so method are now instance side methods. Returning instances is one of the main uses of class side methods. Lookup factory methods, abstract factory, and prototype, for a good example of software patterns that creates instances from the class side. Understanding polymorphism is now very important since all methods on the hierarchy are callable (on both the class and instance side). Even methods that are overridden on a subclass can still be called using “super method”.
This is a fundamental piece of learning Smalltalk and not simple to understand, but it is also one of those things that is really easy to learn, and once you do you really don’t think about it anymore. It just seems natural. (and when it’s missing in other languages you go yuck! Why is this so difficult!). It also means that people that understand it think that it’s simple and natural and may not spend enough time talking about it. J
One more question for you. I mentioned returning a newly created instance or a specific class from a class side method. Can you name another reason why you would write a method on the class side? Why would it be a good idea to put a method on the class side instead of the instance side? (a hint for you, I’m thinking of something where nothing is returned. (of course in Smalltalk if nothing is returned you get back self, what I mean is that nothing useful is returned)) Bonus points for 2 or more answers with or without returning something J.
All the best,
View this message in context: Re: FileDirectory <http://forum.world.st/FileDirectory-tp4897377p4897418.html>
Sent from the Squeak - Beginners mailing list archive <http://forum.world.st/Squeak-Beginners-f107673.html> at Nabble.com.
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the Beginners