Skip to content

Commit

Permalink
frontend: Support own trait calls in default implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
dinfuehr committed Oct 15, 2024
1 parent 89c067b commit 4523034
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 41 deletions.
19 changes: 18 additions & 1 deletion dora-frontend/src/impldefck.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1149,7 +1149,6 @@ mod tests {
}

#[test]
#[ignore]
fn impl_reuse_trait_implementation_with_call() {
ok("
trait Foo {
Expand All @@ -1166,4 +1165,22 @@ mod tests {
}
")
}

#[test]
fn impl_reuse_trait_implementation_with_self_call() {
ok("
trait Foo {
fn f(): Int64;
fn g(): Int64 { self.f() }
}
impl Foo for String {
fn f(): Int64 { 1 }
}
fn x(a: String) {
a.f();
}
")
}
}
60 changes: 20 additions & 40 deletions dora-frontend/stdlib/io.dora
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,26 @@ impl Read for OpenFile {
}

impl Write for OpenFile {
fn write(array: Array[UInt8], offset: Int64, len: Int64): Result[Int64, IOError] {
let written = fileWrite(self.fd, array, offset, len);

if written >= 0 {
Ok[Int64, IOError](written)
} else {
Err[Int64, IOError](IOError())
}
}

fn flush(): Result[(), IOError] {
Ok(())
}
}

pub trait Read {
fn read(array: Array[UInt8], offset: Int64, len: Int64): Result[Int64, IOError];
}

pub trait Write {
fn writeAll(array: Array[UInt8], offset: Int64, len: Int64): Result[(), IOError] {
let mut curr = offset;
let end = offset + len;
Expand All @@ -156,28 +176,6 @@ impl Write for OpenFile {

Ok(())
}

fn write(array: Array[UInt8], offset: Int64, len: Int64): Result[Int64, IOError] {
let written = fileWrite(self.fd, array, offset, len);

if written >= 0 {
Ok[Int64, IOError](written)
} else {
Err[Int64, IOError](IOError())
}
}

fn flush(): Result[(), IOError] {
Ok(())
}
}

pub trait Read {
fn read(array: Array[UInt8], offset: Int64, len: Int64): Result[Int64, IOError];
}

pub trait Write {
fn writeAll(array: Array[UInt8], offset: Int64, len: Int64): Result[(), IOError];
fn write(array: Array[UInt8], offset: Int64, len: Int64): Result[Int64, IOError];
fn flush(): Result[(), IOError];
}
Expand Down Expand Up @@ -213,24 +211,6 @@ impl BufferedWriter {
}

impl Write for BufferedWriter {
fn writeAll(array: Array[UInt8], offset: Int64, len: Int64): Result[(), IOError] {
if self.remainingCapacity() < len {
let result = self.flushBuffer();

if result.isErr() {
return Err(result.getErrOrPanic());
}
}

if len < self.buffer.size() {
Array[UInt8]::copy(array, offset, self.buffer, self.len, len);
self.len = self.len + len;
Ok[(), IOError](())
} else {
self.file.writeAll(array, offset, len)
}
}

fn write(array: Array[UInt8], offset: Int64, len: Int64): Result[Int64, IOError] {
if self.remainingCapacity() < len {
let result = self.flushBuffer();
Expand Down
22 changes: 22 additions & 0 deletions tests/trait/trait-default2.dora
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
trait Foo {
fn f(): Int64 { 0 }
fn g(): Int64 { self.f() + 1 }
}

impl Foo for String {
fn f(): Int64 { 100 }
}

impl Foo for Bool {
fn f(): Int64 { 200 }
}

fn main() {
let a: String = "foo";
assert(a.f() == 100);
assert(a.g() == 101);

let a: Bool = true;
assert(a.f() == 200);
assert(a.g() == 201);
}

0 comments on commit 4523034

Please sign in to comment.