-
-
Save MasterQ32/606a6fc02c16f022137e3925c642bd27 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 1) the trivial case: | |
// this is where your assumptions about any kind of optimizer trigger | |
fn foo(value: AnyType) void | |
{ | |
log.info("value is {}", .{value}); | |
} | |
// 2) the "it gets a tad more problematic" case: | |
// this function is returning what? Also AnyType? Or can i use @TypeOf(value)? Will that return `AnyType`? | |
fn foo(value: AnyType) @TypeOf(value) | |
{ | |
return 2 * value; | |
} | |
// 3) this is where we get borked case: | |
fn Foo(comptime T: type) type { | |
return struct { | |
object: T, | |
}; | |
} | |
fn makeFoo(value: AnyType) Foo(@TypeOf(value)) { | |
return .{ object = value, }; // does this store AnyType? Does it store what we call makeFoo with? | |
} | |
// 4) what? | |
// assuming AnyType is a regular zig type: | |
const Foo = struct { | |
value: AnyType, | |
}; | |
// Assuming AnyType is struct { value: *opaque{}, type: ??? }, this will break: | |
fn moveToHeap(allocator: *std.mem.Allocator, value: AnyType) !*Foo { | |
const foo = try allocator.create(Foo); | |
foo.* = Foo { .value = value }; | |
return foo; | |
} | |
test "this will horribly break" { | |
var x = try moveToHeap(std.testing.allocator, @as(u32, 25)); | |
// x now holds a dangling pointer to a temporary u32 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment