GNU Smalltalk
| GNU Smalltalk | |
|---|---|
| 原著者 | スティーブ・バーン、パオロ・ボンジーニ |
| 初回リリース | 1989年2月6日 |
| 安定版リリース | 3.2.5 / 2013年4月8日 |
| リポジトリ |
|
| オペレーティングシステム | Unix ( Linux、Cygwin、Mac OS X/Darwin ) |
| 種類 | プログラミング言語 |
| ライセンス | GPL + LGPL |
| ウェブサイト | https://www.gnu.org/software/smalltalk/ |
GNU Smalltalkは、 GNUプロジェクトによるSmalltalk プログラミング言語の実装です。
この実装は、他のSmalltalk環境とは異なり、プログラム入力にテキストファイルを使用し、その内容をSmalltalkコードとして解釈します。そのため、GNU Smalltalkは、従来のSmalltalkの環境というよりも、インタープリタのように動作します。
GNU Smalltalkには、SQLite、libSDL、cairo、gettext、Expatなど、多くのフリーソフトウェアライブラリへのバインディングが含まれています。
例
これらの例は、GNU Smalltalk 3.0以降のバージョンでのみ動作します。典型的なHello Worldの例:
'Hello World!' displayNl基本的なSmalltalkコード:
"リテラルを含むすべてがオブジェクトなので、これは動作します:"-199 abs "199"' gst is cool ' size "11"「スリック」 インデックス: $c "4"「いい 天気 だね」asLowercase asSet asSortedCollection asString " ′ ?acdeinsty" コレクション
コレクションの構築と使用:
a := #( 1 'hi' 3.14 1 2 ( 4 5 ) )a at: 3 "3.14" a reverse "((4 5) 2 1 3.14 'hi' 1)" a asSet "Set(1 'hi' 3.14 2 (4 5))"ハッシュの構築と使用:
hash := Dictionary from: { 'water' -> 'wet' . 'fire' -> 'hot' } . hash at: 'fire' "Prints: hot"hash keysAndValuesDo: [ : k : v | ( '%1 is %2' % { k . v }) displayNl ]"出力: water is wet fire is hot"hash removeKey: 'water' "'water' -> 'wet' を削除します"ブロックとイテレータ
ブロックをパラメータで渡してクロージャにする:
"ブロックを記憶する。" remember := [ : name | ( 'Hello, %1!' % { name }) displayNl ] ."適切なタイミングになったら、クロージャを呼び出します!" remember value: 'world' "=> 'Hello, world!'"メソッドからクロージャを返す:
Integer extend [ asClosure [ | value | value := self . ^ { [ : x | value := x ] . [ value ] } ] ] blocks := 10 asClosure .setter := blocks first .getter : = blocks second .getter value "=> 10" setter value : 21 "=> 21" getter value "=> 21" ブロックを使用して呼び出し元に情報を返します。
Integer extend [ ifEven: evenBlock ifOdd: oddBlock [ ^ self even ifTrue: [ evenBlock value: self ] ifFalse: [ oddBlock value: self ] ] ]上記のメソッドを呼び出し、ブロックを渡します。
10 ifEven: [ :n | n / 2 ] ifOdd: [ :n | n * 3 + 1 ] "=> 5"Iterating over enumerations and arrays using blocks:
array := #(1 'hi' 3.14)array do: [ :item | item displayNl ]"=> 1""=> hi""=> 3.14"(3 to: 6) do: [ :item | item displayNl ]"=> 3""=> 4""=> 5""=> 6"A method such as inject:into: can accept both a parameter and a block. It iterates over each member of a list, performing some function on while retaining an aggregate. This is analogous to the foldl function in functional programming languages. For example:
#(1 3 5) inject: 10 into: [ :sum :element | sum + element ] "=> 19"On the first pass, the block receives 10 (the argument to inject) as sum, and 1 (the first element of the array) as element, This returns 11. 11 then becomes sum on the next pass, which is added to 3 to get 14. 14 is then added to 5, to finally return 19.
Blocks work with many built-in methods:
(File name: 'file.txt') withWriteStreamDo: [ :file | file nextPutAll: 'Wrote some text.'; nl ]"File is automatically closed here"(File name: 'file.txt') linesDo: [ :each | each displayNl ]"=> Wrote some text."列挙型とブロックを使用して、1から10までの数字を2乗します。
( 1 から 10まで) collect: [ : x | xの 2乗] "=> [1, 4, 9, 16, 25, 36, 49, 64, 81, 100]"クラス
以下のコードはPersonというクラスを定義しています。Magnitudeから派生することで、1つ(<)を除くすべての比較メソッドが自動的に定義されます。その比較メソッドを追加することで、asSortedCollection年齢でソートできます。 をオーバーライドすることで、オブジェクトの印刷/表示方法(デフォルトではプログラマーによる印刷とユーザーによる表示の表現を共有)をオーバーライドできることに注意してくださいprintOn:
Magnitude サブクラス: Person [ | name age | Person class >> name: name age: age [ ^self new name: name; age: age; yourself ] < aPerson [ ^self age < aPerson age ] name [ ^name ] name: value [ name := value ] age [ ^age ] age: value [ age := value ] printOn: aStream [ aStream nextPutAll: ('%1 (%2)' % { name. age }) ]]group := { Person name: 'Dan' age: 23. Person name: 'Mark' age: 63. Person name: 'Cod' age: 16.}.group asSortedCollection reverseThe above prints three names in reverse age order:
OrderedCollection (Mark (63) Dan (23) Cod (16) )Exceptions
An exception is raised with a halt call:
self haltAn optional message can be added to the exception; there's also error: which raises a different kind of exception:
self halt: 'This is a message'self error: 'This is a message'These are actually wrappers for the actual exception raising method, signal:
Error signalError signal: 'Illegal arguments!'Exceptions are handled by on:do: blocks.
[ something to do ] on: Exception do: [ : ex | handle exception in ex ]もちろん、特定の例外(およびそのサブクラス)のみをキャッチすることもできます。
[ something to do ] on: Warning do: [ : ex | handle exception in ex ]ハンドラー節で使用可能な例外オブジェクトを使用して、最初のブロックを終了または再開することができます。終了がデフォルトですが、明示的に指定することもできます。
[ Error signal: 'foo' ] on: Error do: [ : ex | ex return: 5 ](警告 信号: '次に何を?' ) printNl "=> nil"
[ (警告 信号: '次に何を?' ) printNl ] on: Warning do: [ : ex | ex resume: 5 ] "=> 5"参照
外部リンク
- 公式サイト