すなわち、Smalltalk。 Javaがint
であって、Integer
,ではなく、でなければ、を使用する必要があったものすべてがint
であるとすれば、どうなるか想像してみてください。それはスモールトークです。
これはSqueak 5.0でSmallInteger
クラスを定義するコードの抜粋です。
Integer immediateSubclass: #SmallInteger
instanceVariableNames: ''
classVariableNames: ''
poolDictionaries: ''
category: 'Kernel-Numbers'!
!SmallInteger commentStamp: 'eem 11/20/2014 08:41' prior: 0!
My instances are at least 31-bit numbers, stored in twos complement
form. The allowable range in 32-bits is approximately +- 10^9
(+- 1billion). In 64-bits my instances are 61-bit numbers,
stored in twos complement form. The allowable range is
approximately +- 10^18 (+- 1 quintillion). The actual
values are computed at start-up. See SmallInteger class startUp:,
minVal, maxVal.!
!SmallInteger methodsFor: 'arithmetic' stamp: 'di 2/1/1999 21:31'!
+ aNumber
"Primitive. Add the receiver to the argument and answer with the result
if it is a SmallInteger. Fail if the argument or the result is not a
SmallInteger.
Essential, No Lookup. See Object documentation whatIsAPrimitive."
<primitive: 1>
^super + aNumber! !
!SmallInteger class methodsFor: 'instance creation' stamp: 'tk 4/20/1999 14:17'!
basicNew
self error: 'SmallIntegers can only be created by performing arithmetic'! !
構文や意味論の細部汗をしないでください。あなたはこれから抜け出すべきです:SmallInteger
は、言語の他のすべてのものと同様にオブジェクトクラスとして定義され、算術演算は、言語の他のすべてのコードと同様のメソッドです。しかし、ちょっと奇妙です。それはインスタンス変数を持たず、算術演算を実行することによってのみインスタンスを作成することができ、ほとんどのメソッドは循環的に定義されているように見えます。
"Under the hood"では、インプリメンテーションは算術を適切なマシン命令にマップします(<primitive: 1>
はこれに関する実装のヒントです)。SmallInteger
を整数そのものとして格納します。ハードウェアに比べて制限された範囲は、オブジェクトへのポインタ( "tagged pointers")ではなく、メモリワードを整数としてマークするために2ビットが予約されているためです。
それはプリミティブなし*実装*することはできませんが、あなたがそれらを見かけや言語のユーザーにアクセスできるようにする必要はありません... – assylias
プログラミング言語がある時点でメモリにint型を格納する必要があります。例えばRAM。それはすべてのビットとバイトとすべての言語のフードの下に生のポインタです。 Scalaのような言語は、プリミティブをプログラマに公開しないことを決定する可能性があります。 –
ドキュメントは何も作成しないことがあります。 –