Skip to content

Instantly share code, notes, and snippets.

@skorianez
Created October 3, 2024 14:56
Show Gist options
  • Save skorianez/b9a144e07b742a14df1757ef4215dc5a to your computer and use it in GitHub Desktop.
Save skorianez/b9a144e07b742a14df1757ef4215dc5a to your computer and use it in GitHub Desktop.
Interate over "objects"
package main
import "core:fmt"
base :: struct {
name : string,
}
obj :: union {
circle,
line,
}
// PURE "VIRTUAL" METHOD
obj_name :: proc(o: obj) {
str : string
switch my_obj in o {
case circle:
str = circle_name(my_obj)
case line:
str = line_name(my_obj)
}
fmt.printf("O nome é:%v\n", str)
}
obj_print :: proc {
circle_print,
line_print,
}
// CIRCLE
circle :: struct {
using _base :base,
r : f32,
}
circle_new :: proc(r : f32) -> circle {
return {
name = "circle",
r = r,
}
}
circle_print :: proc(c : circle) {
fmt.printf("%v: raio: %v\n", c.name, c.r )
}
circle_name :: proc(c: circle) -> string {
return c.name
}
// line
line :: struct {
using _base :base,
x,y : f32,
}
line_new :: proc(x,y : f32) -> line {
return {
name = "line",
x = x,
y = y,
}
}
line_print :: proc(l : line) {
fmt.printf("%v: x:%v y:%v\n",l.name, l.x, l.y)
}
line_name :: proc(l : line) -> string {
return l.name
}
// LIST
obj_list :: struct {
objs : [dynamic]obj
}
obj_list_add :: proc(ol :^obj_list, o : obj) {
append(&ol.objs, o)
}
obj_list_print :: proc(ol :^obj_list) {
for objt in ol.objs {
// switch o in objt{
// case circle:
// obj_print(o)
// case line:
// obj_print(o)
// }
obj_print(objt)
}
}
main :: proc() {
// c := circle_new(10)
// l := line_new(1,2)
// obj_print(c)
// obj_print(l)
// obj_name(c)
// obj_name(l)
ol : obj_list
obj_list_add(&ol, circle_new(2))
obj_list_add(&ol, line_new(5,4) )
obj_list_print(&ol)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment