Smalltalk 的异国特色

发布于 2024-09-06 23:49:07 字数 368 浏览 10 评论 0 原文

当您来自一个更像 C 的语法世界时,Smalltalk 语法(和功能)会发现非常奇特(甚至令人不安)。我发现自己在一些事情上浪费了时间
我有兴趣了解与更经典/主流的语言相比,您发现什么真正具有异国情调,并且您认为有助于理解该语言。

例如,使用逻辑运算符求值:

  • (object1 = object2) & (object3 = object4) :这将计算整个表达式,即使左侧部分为 false,其余部分也会被计算。
  • (object1 = object2) and: [object3 = object4] :这将评估左侧部分,并且仅当第一个为 true 时才会评估右侧部分。

Smalltalk syntax (and features) can be found pretty exotic (and even disturbing) when you come from a more C-like syntax world. I found myself losing time with some
I would be interested in learning knowing what you found really exotic compared to more classic/mainstream languages and that you think helps to understand the language.

For example, evaluation with logic operators :

  • (object1 = object2) & (object3 = object4) : this will evaluate the whole expression, even if the left part is false, the rest will be evaluated.
  • (object1 = object2) and: [object3 = object4] : this will evaluate the left part, and only will evaluate the right part if the first is true.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

活泼老夫 2024-09-13 23:49:07

一切都是对象,虚拟机之上的一切都可供检查和修改。 (原语是 VM 的一部分,至少在概念上是这样。)甚至您的调用堆栈也是可用的 (thisContext) - Seaside 在过去通过简单地将调用堆栈向下转换为流并恢复它来实现延续(返回继续部分)只需从该流中读出激活帧即可!

您可以从字符串构造一个选择器,并将其转换为 Symbol 并将其作为消息发送:self Perform: 'this', 'That' 会做同样的事情作为self thisThat。 (但不要这样做,出于同样的原因,您应该在 Lisps 和 PHP 中避免使用 eval:很难调试!)

消息传递:它是 不是方法调用

#become: 可能是对于以前没有见过它的人来说有点震惊。 (tl;dr 两个对象指针的大规模交换 - 所有对 B 的引用现在都指向 A,所有对 A 的引用现在都指向 B)

Everything is an object, and everything above the VM's available for inspection and modification. (Primitives are part of the VM, conceptually at least.) Even your call stack's available (thisContext) - Seaside implemented continuations back in the day by simply swizzling down the call stack into a stream, and restoring it (returning to the continuation) by simply reading out activation frames from that stream!

You can construct a selector from a string and turn it into a Symbol and send it as a message: self perform: 'this', 'That' will do the same thing as self thisThat. (But don't do this, for the same reasons you should avoid eval in both Lisps and PHP: very hard to debug!)

Message passing: it's not method invocation!

#become: is probably a bit of a shock to anyone who hasn't seen it before. (tl;dr a wholesale swapping of two object pointers - all references to B now point to A, and all references to A now point to B)

小情绪 2024-09-13 23:49:07

基元

someMethod
  <primitive 14122 wtf>
  "fail and execute the following"
  [self] inlineCopyInject: [:t1 | self].

Primitves

someMethod
  <primitive 14122 wtf>
  "fail and execute the following"
  [self] inlineCopyInject: [:t1 | self].
我是有多爱你 2024-09-13 23:49:07

我与 Smalltalk 的第一次角力是元类的实现。

考虑一下:

'This is a string' 的类是什么?嗯,类似于String

String 是什么类? 字符串类。注意:这是一个类,但它没有名称,它只是将自己打印为“String class”。

String类是什么类? 元类。注意:这是一个命名类。

Metaclass 是什么类?正如您所期望的(或不期望的),这是元类。其中,正如您所料,该类又是Metaclass

这是第一个循环。另一个我一开始觉得相当深奥的问题(当然,现在我早餐吃元类)是下一个:

String 的超类是什么? Object(最终,Smalltalk 的不同实现具有这些基本类的不同类层次结构)。

Object 的超类是什么? 。现在,这是 Smalltalk 中一个有趣的答案,因为它实际上是一个对象! nil class 回答 UndefinedObject。其中超类是...Object

在那些日子里,浏览超类和关系实例对我来说是一次真正的过山车之旅......

My first wrestling session with Smalltalk was the metaclass implementation.

Consider this:

What is the class of 'This is a string'? Well, something like String.

What is the class of String? String class. Note: this is a class, but it has no name, it just prints itself as 'String class'.

What is the class of String class? Metaclass. Note: this is a named class.

What is the class of Metaclass? As you might expect (or not) this is Metaclass class. Of which, again as you might expect, the class is Metaclass again.

This is the first circularity. Another one which I found rather esoteric at first (of course, now I eat metaclasses for breakfast) is the next one:

What is the superclass of String? Object (eventually, different implementations of Smalltalk have different class hierarchies of these basic classes).

What is the superclass of Object? nil. Now this is an interesting answer in Smalltalk, because it actually is an object! nil class answers UndefinedObject. Of which the superclass is ... Object.

Navigating through the superclass and instance of relations was a real rollercoster ride for me in those days...

去了角落 2024-09-13 23:49:07

选择性断点(我有时实际使用)怎么样:

foo
    thisContext sender selector == #bar ifTrue:[ self halt ].
    ...

将自行调试,但前提是从 bar 调用。如果从无数其他地方调用 foo 并且常规断点太频繁命中,则很有用。

How about selective breakpointing (which I actually use at times):

foo
    thisContext sender selector == #bar ifTrue:[ self halt ].
    ...

will debug itself, but only if called from bar. Useful, if foo is called for from zillion other places and a regular breakpoint hits too often.

倾城月光淡如水﹏ 2024-09-13 23:49:07

我一直很喜欢 Smalltalk quine:

quine
     ^thisContext method getSource

(Pharo 版本。)

I've always been fond of the Smalltalk quine:

quine
     ^thisContext method getSource

(Pharo version.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文