Hetu Script Language
Guide
Grammar
  • Dart APIs
  • Hetu APIs
  • VS Code extension
Github
  • English
  • 简体中文
  • English
Guide
Grammar
  • Dart APIs
  • Hetu APIs
  • VS Code extension
Github
  • English
  • 简体中文
  • English
  • Introduction
  • Identifier & keywords
  • Builtin types
  • Operators
  • Variable
  • Control flow
  • Function
  • Enum
  • Class
  • Struct
  • Type system
  • Future, async & await
  • Import & export
  • Strict mode
  • Assert & error

Class

Class can have static variables and methods. Which can be accessed through the class name.

Member functions can also be declared with get, set, constructor, they means getter, setter and contructor function.

If a class have a getter or setter function. You can use 'class_name.func_name' to get or set the value hence get rid of the empty parentheses.

Constructors can have or have not names and cannot return values. When calling they will always return a instance.

When define constructors with name, can redirecting to default constructor.

You can use this syntax in the parameter to quick initialize the member on instance just like in Dart.

// class definition
class Calculator {
  // static private member
  static var _name = 'the calculator'
  // static get function
  static get name -> string {
    return _name
  }
  // static set function
  static set name(new_name: string) {
    _name = new_name
  }
  // static function
  static function greeting {
    print('hello! I\'m ' + name)
  }

  // instance member
  let x: number
  let y: number

  let birthDate
  // constructor with parameters
  // you can use `this` syntax in the parameter to
  // quick initialize the member on instance
  // just like in Dart
  constructor (this.x: number, this.y: number, age: int) {
    // use this to access instance members shadowed by function parameters
    this.birthDate = Now() + age
  }

  // named constructyor redirecting to default constructor
  constructor fromPosition (position, age): this(position.x, position.y, age)

  function meaning -> number {
    // when there's no shadowing, `this` keyword can be omitted
    return x * y
  }
}

Inheritance

Use 'extends' to inherit other class's members.

class Animal {
  function walk {
    print('animal walking')
  }

  var kind

  constructor (kind) {
    this.kind = kind
  }
}

class Bird extends Animal {
  function animalWalk {
    // You can access a overrided member in super class by the super keyword within a method body.
    super.walk()
  }
  // override super class's member
  function walk {
    print('bird walking')
  }
  function fly {
    print('bird flying')
  }

  // You can use super class's constructor by the super keyword after a constructor declaration.
  constructor _: super('bird')

  // factory is a special kind of contructor that returns values.
  // factory are static and cannot directly access instance members and constructors.
  factory {
    return Bird._()
  }
}

super

Within a instance's namespace, use super to access the super class's members.

class Super3 {
  var name = 'Super'
}
class Extend3 extends Super3 {
  var name = 'Extend'
  function getSuperName() {
    return super.name
  }
}

You cannot use super in static methods.

Type conversion

Outside a class's namespace, use as to convert a sub class to any super class it extends from.

You can then access that specific class's members.

class Super3 {
  var name = 'Super'
}
class Extend3 extends Super3 {
  var name = 'Extend'
}
var a = Extend3()
var b = a as Super3
b.name = 'Changed'
print((a as Super3).name) // 'Changed'

Constructor tear off

Sometimes, we would like to use Class constructors in functional programming situations. However it normally doesn't work because a class name resolves into a 'class' or 'type' object rather than a function which is needed.

However, we can achieve this by accessing the internal name of the constructor($constructor):

class Person {
  var name
  constructor (name) {
    this.name = name
  }
}

final ctor = Person.$constructor
final p = ['jimmy', 'wang', 'naruto']
final objectList = p.map((element) {ctor(element)})

Namespace

It's common in Dart to define a abstract class with only static members for restricting some values or methods to a namespace.

In Hetu script, you can directly create a code block with keyword namespace to achieve this.

The namespace code block only allows for variable/class/function declaration, cannot have import statement or expresssions.

namespace universe {
  var meaning = 42
}

print(universe.meaning)

Refer Do statement for another kind of code block.

Last Updated: 10/10/25, 12:59 PM
Contributors: hythloday@live.com, 邵燃, unknown, Ran Shao, chengfubeiming
Prev
Enum
Next
Struct