Package: Kernel-Objects

Class: APIError

Introduction

Environment: container

APIError is a class to interact with error object returned by Engine.

An APIError object has the following attributes:

  • code: the error code

  • args: the arguments of the error

  • detail: the detailed message of the error

For example:

error := nil.
client := HTTPClient new.
client url: 'http://www.non-exist-host.com/path'.
[ client get ] onErrorDo: [ :ex | error := ex ].
error code = 'http-request-error'.

Instance Method

Category: accessing

  • args - Get error args.

  • code - Get error code.

  • detail - Get error detail message.

Class: Boolean

Introduction

Environment: container

Boolean is a subclass of Object. It has two instances: true and false.

We can use the ifTrue: and ifFalse: methods to evaluate a block depending on the value of the receiver.

count := 0.
lf = 'There is a problem with my internet' ifTrue: [ count := count + 1 ].

lf = 'There is a problem with my internet'
   ifTrue: [ count := count + 1 ]
   ifFalse: [ count := count - 1 ].

We can also use the and: and or: methods to evaluate a block depending on the value of the receiver.

lf = 'a' or: 'lf = 'b'.
lf = 'a' or: ['lf = 'b'].

a > 10 and: a < 20.

Class: Character

Introduction

Environment: container

A Character can represent any single character from the Unicode character set.

We can create a character by using the literal syntax, which is a single character enclosed in single quotes. For example:

aCharacter := $A.

In this example, aCharacter is an instance of Character representing the uppercase letter A.

We can also create a character by using the integer value of a character. For example:

aLineFeed := Character value: 10.

Class Method

Category: accessing untypeable characters

  • cr - Answer the character representing a carriage return.

  • lf - Answer the character representing a linefeed.

  • null - Answer the character representing a null.

  • space - Answer the character representing a space.

  • tab - Answer the character representing a tab.

Category: instance creation

  • value: - Answer the character whose value is an integer.

    For example:

    aLineFeed := Character value: 10.

Instance Method

Category: covnerting

  • asCharacter - Answer the receiver itself.

    For example:

    $a asCharacter = $a.
  • asInteger - Answer the receiver's character code.

    For example:

    $A asInteger = 65.
  • asLowercase - If the receiver is uppercase, answer its matching lowercase Character.

    For example:

    $A asLowercase = $a.
  • asString - Answer the receiver as a string.

    For example:

    $A asString = 'A'.
  • asUppercase - If the receiver is lowercase, answer its matching uppercase Character.

    For example:

    $a asUppercase = $A.

Class: Dictionary

Introduction

Environment: container

A dictionary is a collection of elements that associate a key object with a value object. It can be viewed as a set of associations.

It is used when you need a collection of objects which can be accessed with a key. For example, if you associate some words with a definition, the word will be the key and the definition will be the value. Both of them can be any kind of objects.

Associations are used to fetch the value using a key ( key -> value).

Following common used methods:

  • new create a new dictionary.

  • size return the size of the dictionary.

  • keys return the keys of the dictionary.

  • at: get the value associated with the given key.

  • at:put: set the value associated with the given key.

  • asDictionary return a dictionary containing the elements of the receiver.

aDictionary := {
                   ('key' -> 'value').
                   ('key2' -> 'value2') } asDictionary.
aDictionary at: 'key'. "value"
aDictionary size. "2"
aDictionary keys. "{'key' 'key2'}"
aDictionary at: 'key' put: 'new value'

Class Method

Category: instance creation

  • new - answer a new Dictionary.

Instance Method

Category: accessing

  • at: - answer the value associated with the given key.

  • at:ifAbsent: - Get the value of the parameter with specified name, and specify a default value if absent. The parameter for the default value can be

    • a code block, which will be evaluated if the parameter is absent.

    • a value, which will be returned if the parameter is absent.

    dict := Dictionary new.
    "cash_flow should be 100."
    cash_flow := (dict at: 'cash_flow' ifAbsent: 100).
    dict at: 'cash_flow' put: 200.
    "cash_flow should be 200."
    cash_flow := (dict at: 'cash_flow' ifAbsent: 100).
  • at:put: - Set the value associated with the given key.

  • keys - answer keys as an array in the receiver.

  • size - answer the number of associations in the receiver.

  • values - answer dictionary values as an array in the receiver.

Category: enumerating

  • collect: - Evaluate the given block for each element of the receiver and return a new Dictionary with the results.

    For example:

    ({ ('key' -> 'value').
      ('key2' -> 'value2') } asDictionary collect: [ :key :value | 
     key -> (value , ' new') ]) asJson.
  • detect: - Evaluate aBlock with each of the receiver's elements as the argument until aBlock evaluates to true. Answer the first element for which aBlock evaluates to true.

    For example:

    "Return ('key2' -> 'value2')."
    { ('key' -> 'value').
      ('key2' -> 'value2') } asDictionary detect: [ :key :value | 
     key = 'key2' ].
  • detect:ifFound: - Evaluate aBlock with each of the receiver's elements as the argument until aBlock evaluates to true. If some element evaluates aBlock to true, then cull this element into foundBlock and answer the result of this evaluation.

    For example:

    "Return ('key2_matched' -> 'value2_matched')."
    { ('key' -> 'value').
      ('key2' -> 'value2') } asDictionary detect: [ :key :value | 
     key = 'key2' ] ifFound: [ :key :value | 
     (key , '_matched') -> (value , '_matched') ].
  • detect:ifFound:ifNone: - Evaluate aBlock with each of the receiver's elements as the argument until aBlock evaluates to true. If some element evaluates aBlock to true, then cull this element into foundBlock and answer the result of this evaluation. If none evaluate to true, then evaluate exceptionBlock

    For example:

    "Return ('key2_matched' -> 'value2_matched')."
    { ('key' -> 'value').
      ('key2' -> 'value2') } asDictionary detect: [ :key :value | 
     key = 'key2' ] ifFound: [ :key :value | 
     (key , '_matched') -> (value , '_matched') ] ifNone: [ 
     'defaultKey' -> 'defaultValue' ].
  • detect:ifNone: - Evaluate aBlock with each of the receiver's elements as the argument until aBlock evaluates to true. Answer the first element for which aBlock evaluates to true, or answer the value in none block.

    For example:

    "Return ('defaultKey' -> 'defaultValue')."
    { ('key' -> 'value').
      ('key2' -> 'value2') } asDictionary detect: [ :key :value | 
     key = 'key3' ] ifNone: [ 'defaultKey' -> 'defaultValue'].
  • do: - Evaluate the given block for each element of the receiver.

    Each element is an association, so the block should accept two arguments, the key and the value.

    For example:

    {('key' -> 'value'). ('key2' -> 'value2')} asDictionary do: [:key :value | Console print: 'key: ', key, ' value: ', value. Console cr].

Category: removing

  • removeKey: - Remove the association for the given key.

Category: testing

  • includesKey: - Answer true if the receiver includes the given key.

    For example:

    x := {('key' -> 'value'). ('key2' -> 'value2')} asDictionary .
    (x includesKey: 'key') = true.
    (x includesKey: 'key3') ifFalse: [ 
      x at: 'key3' put: 'a-default-value' ].
    (x at: 'key3') = 'a-default-value'.

Class: JSONReader

Introduction

Environment: container

This is a class to read JSON into Mindscript objects.

For example:

"the following example creates an array from a JSON expression::
JSONReader fromString: ' [ 1,2,3 ] '.
"The next example creates a dictionary (with 'x' and 'y' keys):"
JSONReader fromString: ' { "x" : 1, "y" : 2 } '.

Class Method

Category: convenience

  • fromString: - Read a JSON string and return a Mindscript object.

    Return nil if the string is empty.

Class: ProtoObject

Introduction

Environment: container

ProtoObject establishes minimal behavior required of any object in Engine.

ProtoObject has no instance variables, nor should any be added.

One of the most important methods of ProtoObject is subclass:, which is used to create new classes.

Object subclass: #MyClass
    instanceVariableNames: 'a b c'
    classVariableNames: 'd e f'
    package: 'MyPackage'

And for each object, they have the following default attributes that are implmented in ProtoObject:

  • name the name of the object

  • class the class of the object

  • comment the comment of the object

Instance Method

Category: accessing

  • className - Answer the class name of the object.

    For example:

    12 className = 'Integer'.
    'a' classname = 'String'.
    {} className = 'Array'.
    [] className = 'BlockClosure'
  • nestedAt: - answer the element associated with the given nested keys.

    The keys are an array of strings to represent recursive keys in nested dictionaries.

    For example, let's say object x has attribute a, which is another object has an attribute b, then you can get the value of b like this:

    x := {('a' -> {('b' -> 10)})} asDictionary.
    (x nestedAt: {'a'. 'b'}) = 10.

    For an array or a list, its attributes are indexes of them basing on 1.

    If the key is not found, return nil.

  • nestedAt:ifAbsent: - answer the element associated with the given key.

    The keys are an array of strings to represent recursive keys in nested dictionaries.

    For example, let's say object x has attribute a, which is another object has an attribute b, then you can get the value of b with default value 0 like this:

    x := {('a' -> {('b' -> 10)})} asDictionary.
    (x nestedAt: {'a'. 'b'}  ifAbsent: 0) = 10.
    (x nestedAt: {'a'. 'c'}  ifAbsent: [1 + 1]) = 2.
    y := {{('a' -> 10)} asDictionary }.
    (y nestedAt: {1. 'a'}) = 10.

    For an array or a list, its attributes are indexes of them basing on 1.

    If the key is not found, then return the value in default-value, which can be:

    • a code block or

    • a value.

Category: combining

  • , - Concatenate two objects.

  • -> - answer an Association between self and an object.

    For example:

    ('key' -> 'value').

Category: comparing

  • != - Primitive. This answers whether the receiver and the argument are not equal.

  • = - Primitive. This answers whether the receiver and the argument are equal.

  • == - Primitive. This answers whether the receiver and the argument are the same object (have the same object pointer).

Category: converting

  • asArray - Answer an array to represent an object.

    For example:

    array := 12 asArray. "returns '12'"
    array := 12.3 asArray. "returns {'12.3'}"
    array := {'a'} asArray. "returns {'a'}"
  • asDictionary - answer a Dictionary by converting current object.

  • asJson - Convert the Mindscript object to JSON string.

    For example:

    "obj is a json string '[ 1, 2, 3 ]'"
    obj := {1. 2. 3} asJson.
    " obj2 is a json string '{ "x": 1,"y": 2 }'"
    obj2 := {('x' -> 1). ('y' -> 2)} asJson.
  • asString - Converts an object into a string.

    For example:

    str := 12 asString. "returns '12'"
    str := 12.3 asString. "returns '12.3'"

Category: instance creation

  • new - answer a new instance of the receiver's class.

Category: testing

  • ifNil: - answer the result of evaluating the given block if the receiver is nil.

    For example:

    a := nil.
    a ifNil: [ a := 3 ].
    "Now a is equal to 3."
    
    a ifNil: [ a := 4 ].
    "Now a is still equal to 3."
  • ifNil:ifNotNil: - If the receiver is not nil, pass it as argument to the ifNotNilBlock block. else execute the nilBlock block.

    For example:

    a := 1.
    a ifNil: [ a := 3 ] ifNotNil: [ a := 4 ].
    
    "Now a is equal to 4."
  • ifNotNil: - answer the result of evaluating the given block if the receiver is not nil.

    For example:

    a := nil.
    a ifNotNil: [ a := 3 ].
    "Now a is equal to nil."
    
    a := 3.
    a ifNotNil: [ a := 4 ].
    "Now a is still equal to 4."
  • ifNotNil:ifNil: - If the receiver is not nil, pass it as argument to the ifNotNilBlock block. else execute the nilBlock block.

    For example:

    a := nil.
    a ifNotNil: [ a := 3 ] ifNil: [ a := 4 ].
    
    "Now a is equal to 4."
  • isDefined - Whether an object is an UndefinedObject.

Class: Random

Introduction

Environment: container

I can generate a random number.

For example, to generate a random number between 0 and 6:

" x is a random number between 0 and 6."
x := Random between: 0 and: 6.

Class Method

Category: convenience

  • between:and: - Generate a random number between a range.

    For example, to generate a random number between 0 and 6:

    " x is a random number between 0 and 6."
    x := Random between: 0 and: 6.

Class: Talk

Introduction

Environment: container

My singleton is a central entry point to the system.

I can sleep for 3 seconds by Talk sleepForSeconds: 3.

Class Method

Category: system

  • sleepForSeconds: - sleep for several seconds.

    For example:

    "Sleep 0.1s"
    Talk sleepForSeconds: 0.1

Class: UndefinedObject

Introduction

Environment: container

I describe the behavior of an undefined variable.

You can determine whether a variable has been defined or not by sending the message isDefined to any object. If the variable is undefined, you can expect a false response to that message.

It's important to make sure that all variables are properly initialized before they are used, to avoid errors and unexpected behavior in your program.

b := 10.
"As a is not defined before, the following code block will not be evaluated."
a isDefined ifTrue: [ b := b + 1].
"b is still 10 as a is undefined."
a := 1.
"After a is defined."
a isDefined ifTrue: [ b := b + 1].
"Now b is 11."

This feature is extremely useful when establishing rules for a specific subject.

When a variable is defined within a subject, it can be used in another subject within the same conversation, assuming it was defined earlier.

This demonstrates that variables can be accessed at the conversation level.

Instance Method

Category: testing

  • isDefined - Whether an object is an UndefinedObject.

Last updated