TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

Why I love Smalltalk

86 点作者 mordaroso将近 14 年前

5 条评论

hboon将近 14 年前
It covers some of the minimal syntax of Smalltalk but didn't touch on Smalltalk images, which is a major signature of Smalltalk.<p>A Smalltalk image contains every Smalltalk object. This includes classes (which are Smalltalk objects), instances of the classes, source code, the current running state, everything. You work within the image and interact with it.<p>Think of the image as a copy of your IDE, along with the compiler, your current project's source code, the compiled classes, methods, everything. You can save your image (state) at any time, not unlike the sleep feature of Windows and Mac. It's a live environment. During a programming session, you edit and save the code of class (it's automatically compiled) and every instance of that class is updated. This sounds like what you commonly have in many dynamic languages, but Smalltalk takes it to an extreme. You can re-configure your "IDE" as you go along since they are running in the same image, all the classes and objects are accessible. Or you can set breakpoints and step through code, changing code on the fly and resuming, all without stopping the "program" and starting again. Not unlike a graphical REPL.<p>Does anyone know if there's a similar tool utilizing the same concept other than Self?
评论 #2822284 未加载
draegtun将近 14 年前
<i>The syntax is quite unique to Smalltalk. The message, otherwise known as “method call” in other languages, is called show: (including the colon) and it takes an argument.</i><p><i>self</i> also uses this method call syntax. However this syntax can get quite confusing, see this example from <a href="http://en.wikipedia.org/wiki/Self_(programming_language)" rel="nofollow">http://en.wikipedia.org/wiki/Self_(programming_language)</a><p><pre><code> valid: base bottom between: ligature bottom + height and: base top / scale factor. </code></pre> Smalltalk version provided gets even more verbose:<p><pre><code> valid := self base bottom between: self ligature bottom + self height and: self base top / self scale factor. </code></pre> Personally I prefer Io message syntax because it has far less ambiguity. See <i>Why not use an infix message syntax like Smalltalk?</i> in the FAQ - <a href="http://iolanguage.com/about/faq/" rel="nofollow">http://iolanguage.com/about/faq/</a>
评论 #2823490 未加载
评论 #2821844 未加载
评论 #2822272 未加载
评论 #2822301 未加载
JoachimSchipper将近 14 年前
Copy of the text, since it's hard to reach the site itself [any layout errors should be assumed to be mine]:<p>This post was extracted from a small talk I gave at Simplificator, where I work, titled “Why I love Smalltalk and Lisp”. There should be another post, “Why I love Lisp” following this one.<p>After I learned my basic coding skill in more or less traditional languages, like C, C++, Python, there were four languages that really tought me something new. Those languages changed my way of thinking and even if I never use them, they were worth learning. They are:<p>* Smalltalk<p>* Lisp<p>* Erlang<p>* Haskell<p>You can probably add Prolog to that list, but I never learned Prolog. This post is about Smalltalk.<p>My goal is not to teach Smalltalk but to show things that you can do with Smalltalk that you can’t do with any other language (disclaimer: surely other languages can do it, and we’ll call them Smalltalk dialects). Nevertheless I need to show you some basics of the language to be able to show you the good stuff, so here we go, a first program:<p><pre><code> 1 + 1 </code></pre> That of course, evaluates to 2. If we want to store it in a variable:<p><pre><code> m := 1 + 1 </code></pre> Statements are finished by a period, like this:<p><pre><code> m := 1. m := m + 1 </code></pre> In Squeak, a Smalltalk implementation, there’s an object called Transcript and you can send messages to it to be displayed on the screen. It’s more or less like a log window. It works like this:<p><pre><code> Transcript show: 'Hello world' </code></pre> and it looks like this:<p>[Squeak transcript showing the result of Transcript show: 'Hello World']<p>The syntax is quite unique to Smalltalk. The message, otherwise known as “method call” in other languages, is called show: (including the colon) and it takes an argument. We can run it 10 times in a row with the following snippet:<p><pre><code> 10 timesRepeat: [ Transcript show: 'Hello world' ] </code></pre> There you can start to see how Smalltalk is special. I’m sending the message timesRepeat: to the object 10, an Integer. Doing something N times repeatedly is handled by the Integer class, which if you think about it, makes sense.<p>The second interesting part, is the block. The part inside squared brackets. You might thing that’s the equivalent of other language’s block syntax, like in this Java example:<p><pre><code> for(int i=1; i&#60;11; i++) { System.out.println("Hello world"); } </code></pre> but Smalltalk version’s is a bit more powerful. It’s a real closure. Look at this:<p><pre><code> t := [ Transcript show: 'Hello world' ] </code></pre> Now I have a variable named t, of type BlockClosure, and I can do anything I want with that variable. If I send it the class message it’ll return it’s class:<p><pre><code> t class </code></pre> and if I sed it the value message, it’ll execute and leave a “Hello World” in the transcript:<p><pre><code> t value </code></pre> Let’s see some more code. A message without any arguments:<p><pre><code> 10 printString </code></pre> a message with one argument:<p><pre><code> 10 printStringBase: 2 </code></pre> and a message with two arguments:<p><pre><code> 10 printStringBase: 2 nDigits: 10 </code></pre> Isn’t it cute? That method is called printStringBase:nDigits:. I never seen that syntax anywhere else; well, except in Objective-C, which copied it from Smalltalk.<p>Enough toying around, let’s start building serious stuff. Let’s create a class:<p><pre><code> Object subclass: #MyClass instanceVariableNames: '' classVariableNames: '' poolDictionaries: '' category: 'Pupeno' </code></pre> Notice that a class is created by sending a message to another class telling it to subclass itself with the name and a few other arguments. It’s a message, a method call like any other. Object is a class, classes are objects. The object model of Smalltalk is a beauty but that’s a subject for another post.<p>Now that we have a class, let’s create a method called greet: in that class.<p><pre><code> greet: name "Greets the user named name" | message | message := 'Hello ', name. Transcript show: message. </code></pre> In that method definition first we have a comment for the method, then the list of local variables within pipes (“|”), and then the implementation, which sets the variable message to contain “Hello ” and the comma concatenates name to it. Then we just send it to the transcript.<p>It looks like this:<p><pre><code> MyClass greet method </code></pre> Ok, let’s use it:<p><pre><code> m := MyClass new. m greet: 'Pupeno' </code></pre> To create an object of class MyClass, we send the new message to that class. There’s no new keyword like in Java. new is just a method. You can read it’s code, override it, etc. Don’t mess with it unless you really know what you are doing.<p>Actually, if you think about it, we haven’t seen a single keyword. Look all the code we wrote without having to memorize any keywords! What’s even more important is that by now you essentially now Smalltalk. That’s all there is, but like LEGO bricks, this simple and small building blocks allow you to build whatever you want.<p>Yes, that’s it, that’s all there is to it. We already saw that Smalltalk doesn’t need loops, it has integers and that class implements the timesRepeat: message which allows you to do something N times. There are many other looping methods here and there.<p>What about the if keyword you ask? Surely Smalltalk has an if? Well, no, it doesn’t. What you can recognize as an if is actually implemented in Smalltalk using the same mechanism of classes and message passing you saw already. Just for fun let’s re-implement it.<p>We starte by creating the class PBoolean and then two classes inheriting from it, PTrue and PFalse.<p><pre><code> Object subclass: #PBoolean instanceVariableNames: '' classVariableNames: '' poolDictionaries: '' category: 'Pupeno' PBoolean subclass: #PTrue instanceVariableNames: '' classVariableNames: '' poolDictionaries: '' category: 'Pupeno' PBoolean subclass: #PTrue instanceVariableNames: '' classVariableNames: '' poolDictionaries: '' category: 'Pupeno' </code></pre> For the class we created before, MyClass, we define a equals: method that will return either true or false, or rather, PTrue or PFalse.<p><pre><code> equals: other ^ PTrue new </code></pre> The little hat, ^, means return. For now, just a hardcoded true. Now we can do this in the workspace:<p><pre><code> m1 := MyClass new. m2 := MyClass new. m1 equals: m2 </code></pre> and get true, that is PTrue, as a result. We are getting close but no if yet. How should if look like? It’ll look like something like this:<p><pre><code> m1 := MyClass new. m2 := MyClass new. (m1 equals: m2) ifTrue: [ Transcript show: 'They are equal'; cr ] else: [ Transcript show: 'They are false'; cr ] </code></pre> and you can start to imagine how to implement it. In PTrue we add the method:<p><pre><code> ifTrue: do else: notdo ^ do value </code></pre> That method basically takes two parameters, evaluates the first one and ignores the second one. For PFalse we create the oposite:<p><pre><code> ifTrue: notdo else: do ^ do value </code></pre> and that’s it. A working if! If you ask me, I think this is truly amazing. And if you check Squeak itself, you’ll find the if is actually implemented this way:<p><pre><code> True's ifTrue:ifFalse: </code></pre> If your programming language allows you to create something as basic as the if conditional, then it allows you to create anything you want.
评论 #2821628 未加载
评论 #2821954 未加载
评论 #2822247 未加载
redxaxder将近 14 年前
<p><pre><code> equals: other ^ PTrue new </code></pre> The implementation of ifTrue isn't done. The branching was just deferred to the equals function, which has to somehow choose between PTrue and PFalse.<p>Will it take advantage of the ifTrue function you're building? Will it resort to some nasty arithmetic? Will it call some baked-in feature of smalltalk? Without an answer here, the "implementation" of <i>if</i> is just an illusion.
评论 #2822010 未加载
评论 #2822310 未加载
评论 #2822038 未加载
评论 #2823265 未加载
xradionut将近 14 年前
I like Smalltalk, but like Forth and Lisp it's just not practical in most of my computing tasks, career or hobby-wise. It's a shame that Smalltalk didn't evolve enough to challenge Java in the '90s. But my buds that knew it well and also had some C chops are in demand as Objective C coders.
评论 #2822583 未加载
评论 #2822333 未加载