Custom Buffer Type using Parametric Polymorphism #3888
Replies: 2 comments
-
FixedSizeBuffer :: struct($E: typeid, $SIZE: u64) {
count: u64,
buffer: [SIZE]E,
}
// Didn't need a pointer
get_capacity :: proc(buffer: $B/FixedSizeBuffer) -> u64 {
return B.SIZE
}
// Didn't need a pointer
get_count :: proc(buffer: $B/FixedSizeBuffer) -> u64 {
return buffer.count
}
// What was $T2 supposed to be? You can't have a $ return value.
get_slot :: proc(buffer: ^$B/FixedSizeBuffer($E, $S), index: u64) -> ^E {
if index < buffer.count {
return &buffer.buffer[index]
}
return nil
}
_main :: proc() {
fsb: FixedSizeBuffer(string, 5)
fmt.println(fsb)
fmt.println(get_capacity(fsb))
fmt.println(get_count(fsb))
fmt.println(get_slot(&fsb, 2)) // returns nil because buffer.count < 2
} |
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by
Kelimion
-
Please don't ping us in two places (Discord and GitHub) about the same question. |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
I have the following struct (for interop purposes):
And I have a few different procedures for it (examples):
I would like to create this procedure:
And use it like this:
But this throws the following error:
Can someone please help me out with this.
Beta Was this translation helpful? Give feedback.
All reactions