Skip to content

Introduction

qrai edited this page Jul 18, 2022 · 2 revisions

Enums

Enums is basic concept of Object-oriented programming. Enusm basically represents a type of variable that can be one of defined in Emum values. To create a enum type in ObjectScript, just use construction enum NAME {} and in curly brackets write possible values, separated by |:

enum Gender {
    Male | Female
}

Congratulations, you are defined a enum! Now you simply can check if string value is enum value using is operator:

enum Gender {
    Male | Female
}
print('Male' is Gender) // true
print('Maleeee' is Gender) // false

Typechecking (or a Duck typing)

The ObjectScript most basic concept is duck typing. What is duck typing? Well, that's a form of type-checking, to check a type of value, you simply check for it's fields/methods.

In ObjectScript language there's a typechecks to validate values. To define typecheck:

typecheck isHello -> (typeof value === 'string' && value.startsWith('Hello')

print(isHello('Hello world!')) // true
print(isHello ('Bye world!')) // false

Types

In ObjectScript, Type is basically a class. In type you can define a fields, methods and then create a instance of that type. To defined a type, just use typedef construction:

typedef Person {
    field name
    field age
}
new Person()

Also you can define a constructor of type that will be called when new instance of type will be created:

typedef Person {
    field name
    field age
    ctor(name, age) {
        // 'this' is new instance of type
        this.name = name
        this.age = age
    }
}
new Person('Jake', 20)

Also for type-safety of fields you can specify a typechecks. Typecheck can be as predefined typecheck, as another type or as enum. In our example we will use static type-checking, for checking a primite values types:

typedef Person {
    // Value should be a string primitive 
    field name -> String
    // Value should be a number primitive
    field age -> Number

    ctor(name, age) {
        this.name = name
        this.age = age
    }
}

new Person('Jake', 20)
new Person(2000000, null) // Error...
Clone this wiki locally