Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Structs/Traits/Implementations #38

Open
jamiebuilds opened this issue Jul 3, 2019 · 1 comment
Open

Structs/Traits/Implementations #38

jamiebuilds opened this issue Jul 3, 2019 · 1 comment

Comments

@jamiebuilds
Copy link
Owner

jamiebuilds commented Jul 3, 2019

Structs

struct Employee {
  name: String,
  title: String,
}

let employee = Employee {
  name = "Jamie Kyle",
  title = "Principal Engineer",
}
employee.name
employee.title

Traits

trait Display {
  display: fn (self): String
}

trait Debug {
  debug: fn (self): String
}

Display.display(valueThatImplementsDisplay) # => String
Debug.debug(valueThatImplementsDebug) # => String

StructThatImplementsDisplay.display(structValue) # => String
StructThatImplementsDebug.debug(structValue) # => String

valueThatImplementsDisplay.display() # => String
valueThatImplementsDebug.debug() # => String

Implementations of Structs

implement Employee {
  new = fn (name: String, title: String) {
    return Employee { name, title }
  }

  changeTitle = fn (self, newTitle: String) {
    return Employee {
      name: self.name,
      title: newTitle,
    }
  }
}

let employee = Employee.new(
  name = "Jamie Kyle",
  title = "Principal Engineer",
)
let newEmployee = employee.changeTitle("Shitposter Extraordinaire")

Implementations of Traits

implement Employee as Display {
  display = fn (self) {
    return "{self.name} ({self.title})"
  }
}

let employee = Employee {
  name = "Jamie Kyle",
  title = "Principal Engineer",
}
employee.display() # "Jamie Kyle (Principal Engineer)"

Implementations of Structs and Traits

implement Employee as Display {
  new = fn (name: String, title: String) {
    return Employee { name, title }
  }

  changeTitle = fn (self, newTitle: String) {
    return Employee {
      name: self.name,
      title: newTitle,
    }
  }

  display = fn (self) {
    return "{self.name} ({self.title})"
  }
}

let employee = Employee.new(
  name = "Jamie Kyle",
  title = "Principal Engineer",
)
let newEmployee = employee.changeTitle("Shitposter Extraordinaire")
newEmployee.display() # "Jamie Kyle (Shitposter Extraordinaire)"

Implementations of multiple Traits

implement Employee as Display, Debug {
  display = fn (self) {
    return "{self.name} ({self.title})"
  }

  debug = fn (self) {
    return "Employee \{ name = \"{self.name}\", title = \"{self.title}\" \}"
  }
}

let employee = Employee {
  name = "Jamie Kyle",
  title = "Principal Engineer",
}
employee.display() # "Jamie Kyle (Principal Engineer)"
employee.debug() # "Employee \{ name = \"Jamie Kyle\", title = \"Principal Engineer\" \}"
@jamiebuilds
Copy link
Owner Author

Note: This is a complete rip-off of Rust because out of similar patterns, I like theirs best.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant