Ki

Fundamental Construct

Everything is a block.

Motivation

Iteration

Iteration is heavily based on generators.

generator expression:

[e.name for e in self.employees]

for in (for each):

fn print_employees() {
    for Employable e in self.employees {
        echo('${e.name} - SSN: ${e.ssn}')
    }
}

apply:

fn employee_names() [string] {
    return apply(self.employees, func(Employable e)  {
        return e.name
    })
}

reduce:

fn total_age()  {
    return reduce([e.age() for e in self.employees()], +)
}

filter:

fn find(EmployeeMatcher matches) [Employable] {
    return filter(self.employees, matches)
}

range generator expression:

fn print_evens() {
    for (var i in [0..10]) {
        echo('${i}')
    }
}

Allocation

Allocation:

heap var player = Player()

or:

heap Player player

Deallocation:

free(player)

Attachment:

attach player, enemy: game.add_player_and_enemy(player, enemy)

or:

attach player, enemy {
    game.add_player_and_enemy(player, enemy)
}

Group Operators

Braces: {}

Braces first and foremost declare a block. A block must have a type, and each type has its own semantics. Blocks are also their own scope.

Brackets: []

In C, brackets are used to define array sizes and for array indexing. However, braces are used for initializers.

In Python, brackets are used as list literals/initializers and for all indexing (dicts, tuples, etc.).

I think it's consistent to keep using brackets for all indexing. I also think that C's use of braces as initializers is surprising, considering it's pretty rare in other languages. So I think think brackets should be used for all collections: literals, initializers, indexing, and sizing.

The one exception is a map; map initializers should use braces.

Pros:

  • Consistent with other popular languages (Python, Ruby, JavaScript, Go).
  • Maps are a lot closer to blocks than they are to sequences, because they're essentially namespaces.

Cons:

  • Inconsistent with other collections.
  • The similarity with the fundamental block construct gives a slight impression that maps are more "official", which might lead to overuse.

Brackets are used for arrays; they specify array size and can be used as an

Parentheses: ()

Parentheses are used for function calls, function argument lists, and evaluation precedence.

Angle Brackets: <>

Angle brackets are used for type information.