Skip to content

Commit

Permalink
Use createFmt for neater examples (#182)
Browse files Browse the repository at this point in the history
  • Loading branch information
gatesn authored Oct 4, 2023
1 parent 1576190 commit 173158c
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 9 deletions.
5 changes: 1 addition & 4 deletions example/classes.zig
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,7 @@ pub const User = py.class(struct {

greeting: py.property(struct {
pub fn get(self: *const Self) !py.PyString {
var g = try py.PyString.create("Hello, ");
g = try g.append(self.name);
g = try g.appendSlice("!");
return g;
return py.PyString.createFmt("Hello, {s}!", .{try self.name.asSlice()});
}
}) = .{},
});
Expand Down
9 changes: 4 additions & 5 deletions example/modules.zig
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,10 @@ pub fn hello(
self: *const Self,
args: struct { name: py.PyString }, // (5)!
) !py.PyString {
var str = try py.PyString.create("Hello, ");
str = try str.append(args.name);
str = try str.appendSlice(". It's ");
str = try str.append(self.name);
return str;
return py.PyString.createFmt(
"Hello, {s}. It's {s}",
.{ try args.name.asSlice(), try self.name.asSlice() },
);
}

pub const submod = py.module(struct { // (6)!
Expand Down

0 comments on commit 173158c

Please sign in to comment.