Skip to content

[proposal] new interface in deeplang #28

@chinesebear

Description

@chinesebear
  • discuss interface solution
  • update deeplang SPEC
  • plan of interface implementation

非侵入式接口

interface I {
    fun f()
    // var f: ()->unit
}

class Foo {
    fun f() {
        println("")
    }
}

class Bar implements I {
    fun f()
}

fun useI(i: I) {
    i.f()
}

useI( new Foo() )

struct String {
    fun f() {}
}

enum Color {
    fun f() {}
}

扩展

extension

在原来类型的基础上,添加新的函数。

class Foo {
}

extension Foo {
    fun run() {
        this.xxxx
    }
}
/*
fun Foo_run(this: Foo) {

}
*/

extension Foo {
    fun f() {}
}

fun main() {
    let x = new Foo()
    x.run()
    useI( new Foo() )
}

extension Foo implements I {
    fun f() {}
}

extension String {
    fun reverse() -> String {
        .....
    }
}

"123".reverse()

委托

将一个子字段field的成员(field,method)都委托到当前层级。

class A {
    let x: Int
    fun hello() {
        ......
    }
    fun f() {}
}

// class B : A {}

class B {
    as let a: A
}

let b = new B()
b.x // b.a.x
b.hello() // b.a.hello()
b.f()

useI( b )

// 如果委托的字段,有重现同名的成员(field,method),这个同名的成员就不会被委托。
class C {
    fun f() {}
}

class B {
    as let a: A
    as let c: C
    fun f() {
        c.f()
    }
}

struct D {
    fun f() {}
}

struct E {
    as let d: D
}
type A struct {
    v int
}

type B struct {
    A
}

Metadata

Metadata

Labels

enhancementNew feature or request

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions