Skip to Content

Structs and methods

Focus: one Task struct and one method that returns an updated copy.

Open in Playground 

task_methods.slx
struct Task { id: u64, title: string, done: bool } fn (task Task) mark_done() -> Task { return Task { id: task.id, title: task.title, done: true } } entry complete_task(id: u64, title: string) -> Task { let task: Task = Task { id, title, done: false } return task.mark_done() }

How it works

  • Task is the contract’s typed return value.
  • fn (task Task) mark_done() -> Task declares a method with task as the receiver.
  • complete_task(id, title) creates a new task with done: false.
  • task.mark_done() returns a new Task with the same id and title and done: true.
Last updated on