Adding repr implementation to print structs

Can you print out a struct like:
@value
struct MyPair:
var first: Int
var second: Int

fn __repr__(self):
print("first:", self.first)
print("second:", self.second)

fn main():
print(MyPair(2, 4))
@value
struct MyPair:
var first: Int
var second: Int

fn __repr__(self):
print("first:", self.first)
print("second:", self.second)

fn main():
print(MyPair(2, 4))
1 Reply
Jack Clayton
Jack ClaytonOP16mo ago
Not yet this is blocked by traits, which are currently being worked on, for now you can create a separate method:
@value
struct MyPair:
var first: Int
var second: Int

fn print_all(self):
print("first:", self.first)
print("second:", self.second)


fn main():
MyPair(2, 4).print_all()
@value
struct MyPair:
var first: Int
var second: Int

fn print_all(self):
print("first:", self.first)
print("second:", self.second)


fn main():
MyPair(2, 4).print_all()
Want results from more Discord servers?
Add your server