Back to Basics: A Guide to Variables and Types in ObjectSense
Welcome to your ObjectSense journey! Think of learning any new programming language as assembling a toolkit. The most fundamental items in that kit are variables and types—the containers and the tools themselves.
In Object Sense (OSE), a variable is like a labeled box where you can store information, and a "type" defines what kind of information you can put in the box. Mastering these two concepts is the first and most essential step to building anything with OSE.
Storing Your Data: An Introduction to Variables
In OSE, variables are the basic units for holding data. You declare a variable using the let keyword, following this simple syntax:
let {variable_name} = {expression}
The {variable_name} is the label for your box, and the {expression} is the value you put inside it.
Variable names must follow a few simple rules:
●They can contain letters, numbers, and underscores
●They cannot start with a number
●They are case-sensitive
●They should be descriptive to make your code easy to read
Here's a classic first example:
let world = "ObjectSense!"
echo "Hello ". world
Beyond standard variables, OSE also provides a few specialized types:
●Inline Variables: Defined with Let >> {name} = {expr} and used with Let << {name}.
●Static Constants: Defined with Let! {name} = {constants}. These are useful for values that should never change, like a configuration setting or mathematical constant.
●Object Instances: Defined with Let {name} = {ClassName}() to create an instance of a class.
The Building Blocks: OSE Primitive Types
Now that we have our "boxes" (variables), let's look at the kinds of "tools" and "materials" (types) we can put inside them.
Numbers
OSE supports both integers and floating-point numbers.
Integers can be written in several formats:
// Standard decimal
let s:num = 65535
// Binary (starts with 0b)
let s:numB = 0b1001
// Hexadecimal (starts with 0x)
let s:numHex = 0xff
Floating-point numbers support scientific notation:
let s:fnum = 3.14159
let s:fnum_scientific = 2.99792458e8 // Speed of light
Strings
Strings are used to store text.
let s:str = 'HelloWorld'
// Strings can also handle escape sequences like quotes and newlines
let s:strConvertable = "\"HelloWorld\"\n"
Booleans
Booleans represent true or false values. OSE uses v:true and v:false. When evaluating integers, 0 is treated as false, and any other integer is treated as true.
Collections: Lists and Dictionaries
Collections are types that can hold multiple values.
A List is an ordered sequence of items, like an array. It can hold items of any type.
let list = [0, 1, 2, 3]
echo list[0] // Access by index, prints 0
A Dictionary is a collection of key-value pairs, where you access values using their string key.
let dict = {'x': 1, 'y': 2}
// Access with bracket notation
echo dict['x'] // Prints 1
// Or access with dot notation
echo dict.x // Also prints 1
Putting It All Together: A Practical Example
Let's combine these concepts into a simple, practical example that groups related information about a student.
let studentName = "Alice"
let studentAge = 20
let courses = ["Math", "Science", "History"]
// Use a dictionary to organize all the student's data
let studentInfo = {
'name': studentName,
'age': studentAge,
'courses': courses
}
// Now we can easily access and print the information
echo studentInfo.name . " is " . studentInfo.age . " years old."
echo "Courses: " . string(studentInfo.courses)
This example shows how you can define variables of different types and then compose them into a more complex structure like a dictionary.
With a solid grasp of variables and types, you now have the fundamental building blocks for learning ObjectSense. In the next steps, you'll learn how to use these blocks to create logic and control the flow of your programs.
Top comments (0)