Frequently Asked Question

Frequently Asked Question

How to include ' in a string

  • Double ' means one '.

"a Mindscript string: There's a problem."
'There''s a problem.'

How to include " in a comment

  • !" means one ".

"This is a comment, !" still in a same comment block."

How to repeat executing a block

"Print 1 2 3 4 5"
1 to: 5 do: [:i | Console print: i, ' '].

"Print 0 2 4 6"
0 to: 6 by: 2 do: [:i | Console print: i, ' '].

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

i := 1.
[ i > 10 ] whileFalse: [ Console print: i, '. '.
  i := i + 1 ].

How to check whether a variable is defined

We can use the message isDefined, which is available for all objects.

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.

NOTE

If we declare a variable within a code block, its scope is limited to that specific block, rendering it invisible to other parts of the code. Conversely, A variable established outside the scope of any specific block of code can be accessed by other rules within the conversation, even if those rules pertain to distinct subjects.

For example, if a Mindscript rule has the following code:

a := {  }.
a ifNil: [ 
  b := 10.
  a := b ].

The variable a is accessible to other rules within the scope, whereas the visibility of the variable b is confined solely to the local code block in which it resides.

How to define a local function

In Mindscript, a local function is synonymous with a code block. This block can be executed by sending it the message value, value:, or value:value:.

For example:

aBlock := [ 1 ].
"Call this block by method `value`"
aBlock value = 1.

aBlock := [ :x | x + 1 ].
"Call this block by method `value:`"
(aBlock value: 3) = 4.

aBlock := [ :x :y | x + y + 1 ].
"Call this block by method `value:value:`"
(aBlock value: 3 value: 2) = 6.

How to insert a dynamically generated JSON string into the request body for a webhook

Consider a scenario where you possess a JSON object stored in a MindScript variable named jsonObject, and your aim is to embed this JSON object into the body of a webhook request. To accomplish this, you can leverage the asJson method from MindScript, seamlessly incorporating it into a mustache template as follows:

{
"json": {{$ jsonObject asJson}} 
} 

Troubleshooting Guide

Resolving 'No Implementation for obj: x and behavior-name y' Error

We should examine the Mind Expression Doc, especially the sections labeled Package, to determine the appropriate Mindscript classes and behaviors.

Also, if x is NIL, we need to ensure it's been assigned an accurate value.

Handling "junk in string" error in Mindscript

Problem: The need is to convert a parameter 'x' to an integer in Mindscript. However, the following code throws an error: 'junk in string "x"'.

num := Parameter at: 'x' asInteger.

Answer: In Mindscript, a unary message assumes priority over a keyword message. The execution of the given code, when the explicit parentheses are added, can be illustrated as follows:

num := Parameter at: ('x' asInteger).

To make the keyword message work as intended, parentheses are used for grouping. This refactors the code as follows:

num := (Parameter at: 'x') asInteger.

Hence, using parentheses to group your code elements correctly is critical for ensuring that the code executes as you intend. Understanding and using operator precedence is key in coding, especially in Mindscript where message precedence can alter the functionality of the code.

Last updated