dexscript / design

DexScript - for Better Developer EXperience
http://dexscript.com
Apache License 2.0
4 stars 0 forks source link

Type System #25

Open taowen opened 5 years ago

taowen commented 5 years ago

The goal of the type system is to encourage local abstraction, generalization, and explicitness.

The built-in primitive type and imported java type are "nominal".

The function and interface are "structurally typed". When function referenced as type, an implicit interface will be defined as the same name, covering all variants of the function.

[type1, type2], type[] and {[key: keyType]: valueType} are type operator to construct new type from existing type either the type is nominal or structural.

Builtin Primitive Type

Java Type

java type can be imported by import statement

As function argument

function SayHello(msg: string) {
}

Construct new instance by

out := StringBuilder{}

Java interface can not create new instance, can be used as argument type and variable type

DexScript Function Type

defined by any function

function Hello() {}

function can have overload by its argument type

function Hello(arg: int32) {}
function Hello(arg: int64) {}

function can have overload by where

function IsVip(customer): boolean {
  return customer.name() == 'abc'
}
function Hello(customer) where IsVip(customer) {}

DexScript Interface Type

defined by interface keyword

interface Sayable (
  Say: (this: Sayable) => void
)

If we have defined a function

function Say(msg: string) {}

This will make string implements the interface Sayable. One interface can require multiple functions available:

interface Number (
  Add: (left: Number, right:Number) => Number,
  Sub: (left: Number, right:Number) => Number
)

The interface can also depend on the value

interface VipCustomer (
  this: VipCusomter,
  GetCustomerName: (VipCustomer) => string,
  GetReferer: (VipCustomer) => string
): boolean {
  if GetCusomterName(this) == 'some_customer_name' { return true }
  if GetReferer(this) == 'some_referer' { return true }
  return false
}

interface can also take type argument

interface List (
  T: type,
  Get: (index: int64) => T
)

interface can be constrained

function Say(messages: List<string>) {
}

the <> is partial construction, only applies to interface. the {} is complete construction.

Magic Function

Following function names have special meaning

This will make dexscript function or java types implement some dexscript interface automatically.

Await

function World() {
  await {
  -> Say(): string {
    return 'hello'
  }}
}

this will make World implement this interface

interface Sayable (
  Say: (Sayable) => string
)

Uniform Function Call

a.b() is same as b(a)