Mind Expression Docs
English
  • Mind Expression Docs
  • Why Mind Expression
  • Quickstarts
    • Create Scope
    • Add Subject
    • Test
    • Audit
  • Concepts
    • Scopes
    • Subjects
    • Components
    • Sandbox
    • Conversation history
    • Webhooks and escalation
    • Knowledge
    • Live Chat
    • Target Messages
    • Analytics
  • How-to Guides
    • Set up webhooks and escalation
    • Manage global keys
    • Customize messages
    • Educate AI
      • Manage ontologies
      • Recognition Check
      • Semantic Relations
      • Manage entities
    • Steps
    • Components
      • Normal Components
        • Conditions
        • Selection Classes
        • Parameter Bundles
      • Response Components
        • Component Actions
    • Build Q&A Subjects
      • Case 1. Online Payment
      • Case 2. Data Plans
    • Build Info Search Subjects
      • Case 1. Data Usage
      • Case 2. Payment History
    • Build Query Freestyle Subjects
      • Case 1. Data Purchase
      • Case 2. Change Mobile Plan
    • Intelligent Process
      • Introduction to Mindscript
      • Mindscript with Mind Expression
      • Frequently Asked Question
      • Package: Collections and Sequenceable
      • Package: Collections Unordered
      • Package: Collections-Strings
      • Package: Engine-Conversation
      • Package: Kernel-HTTP
      • Package: Kernel-Methods
      • Package: Kernel-Numbers
      • Package: Kernel-Dates
      • Package: Kernel-Objects
      • Package: Kernel Exceptions
      • Package: Cryptography
    • Test and debug
    • Integration
      • Mind Expression API
      • Chat Widget
      • LINE
      • Facebook Messenger
      • Viber
      • Instagram
      • Discord
      • WhatsApp
      • Google Sheet
        • Google Sheet Webhook
      • Google Calendar
        • Google Calendar Webhook
        • Google Calendar: Use Cases
    • Audit AI Activities
    • Live Chat
    • Target Messages
    • Back up, import and restore Scopes
  • Reference
    • Glossary
    • API Docs
Powered by GitBook
On this page
  • Class: BlockClosure
  • Introduction
  • Instance Method

Was this helpful?

  1. How-to Guides
  2. Intelligent Process

Package: Kernel-Methods

Class: BlockClosure

Introduction

Environment: container

  • Contains a sequence of operations

  • Defined by Mindscript expressions inside square brackets

  • Permits to defer the enclosed operations until a variant of #value is executed

  • Can have its own arguments and temporaries as a regular method but it also has the ability to use external variables

  • It has enclosing method or block temporaries, arguments, and receiver.

Some message needs a code block as argument. For example, #do: needs a block to be executed for each element of the receiver.

The block is executed with the element as argument.

{1. 2. 3. 4. 5. 6. 7. 8. 9. 10} do: [:each | Console print: each, '. '].

You can also use a local variable in a block:

{1. 2. 3. 4. 5. 6. 7. 8. 9. 10} do: [:each |
  | sum |
  sum := 10 + each.
  Console print: sum, '. '].

We can also use the block as a local variable, and execute it later by sending value to it, or value: with an argument.

aBlock := [ Console print: 'Hello world' ].
"Execute a block"
aBlock value.

"Execute a block with an argument"
aBlock := [:each | Console print: each, '. '].
{1. 2. 3. 4. 5. 6. 7. 8. 9. 10} do: [ :item | aBlock value: item].

Instance Method

Category: accessing

  • asString - answer the code string of this block closure.

  • numArgs - answer the number of arguments of the receiver.

Category: controlling

  • whileFalse: - Ordinarily compiled in-line, and therefore not overridable. This is in case the message is sent to other than a literal block. Evaluate the argument, aBlock, as long as the value of the receiver is false.

    For example:

    i := 1.
    [ i > 10 ] whileFalse: [ Console print: i, '. '.
      i := i + 1 ].
  • whileTrue: - Ordinarily compiled in-line, and therefore not overridable. This is in case the message is sent to other than a literal block. Evaluate the argument, aBlock, as long as the value of the receiver is true.

    For example:

    i := 1.
    [ i <= 10 ] whileTrue: [ Console print: i, '. '.
      i := i + 1 ].

Category: evaluating

  • ensure: - Evaluate a termination block after evaluating the receiver, regardless of whether the receiver's evaluation completes.

  • onErrorDo: - Evaluate an error handling block when there is an exception when evaluating the receiver.

    For example, we can use onErrorDo: to handle an error when divide by zero.

    
    a := 1.
    aBlock := [ a := a / 0 ].
    
    aBlock onErrorDo: [ :ex | a := 0].
  • value - Evaluate the receiver and answer the result.

  • value: - Evaluate the receiver with the given argument and answer the result.

  • value:value: - Evaluate the receiver with the given arguments and answer the result.

  • value:value:value: - Evaluate the receiver with the given arguments and answer the result.

  • value:value:value:value: - Evaluate the receiver with the given arguments and answer the result.

PreviousPackage: Kernel-HTTPNextPackage: Kernel-Numbers

Last updated 1 year ago

Was this helpful?