#Matrix thread from #zig

1 messages · Page 1 of 1 (latest)

native hound
#
fn MultiplyType(comptime Self: type, comptime Other: type) type {
  if (Self{}.height != Other{}.height) {
    @compileError("matrices with different heights provided");
  }
  return Matrixx(Other{}.width, Self{}.height);
}

pub fn multiply(self: @This(), other: anytype) MultiplyType(@This(), @TypeOf(other)) {
  const new = MultiplyType(@This(), @TypeOf(other)){};
  // do multiply op on new
  return new;
}
fluid relic
#

self{}.height

#

doesn't seem to be valid

native hound
#

it's Self{}.height the Self is type

#

might need to put inside parenthesis maybe (Self{}).height

fluid relic
#

yes that was it

native hound
#

you can also give the matrix type pub const Height = height; decl so you can do also Self.Height skipping the need of doing {} :P

fluid relic
#

is (self{}) just casting it to its data or something?

native hound
#

{} makes instance of the type

fluid relic
#

oh

native hound
#

() is only needed to disambuage the grammar for compiler

fluid relic
#

and that only works because width and height are comptime

#

because its part of the type

native hound
#

yup

#

all functions that return type are always run fully comptime

fluid relic
#

do i need to set the value of buffer?

#

oh

#

i think i can just set it to undefined in the defenition

#

so its undefined at comptime i think???

#

even though the field isn't comptime

native hound
#

maybe better would give the struct the pub const as I mentioned earlier

#

the undefined default value is not good pattern

#
return struct {
  pub const Width = width;
  pub const Height = height;
  ...
#

then you can Self.Height, Other.Width and so on

#

without having to instantiate the type or set .buffer to undefined every time or worse as default value

#

as general rule it's almost never good idea to have undefined as default value but ask for the programmer instead to set something to undefined by themselves at calling level

#

in the multiply function for example I would set it to undefined manually:

pub fn multiply(self: @This(), other: anytype) MultiplyType(@This(), @TypeOf(other)) {
  const new: MultiplyType(@This(), @TypeOf(other)) = .{ .buffer = undefined };
  // do multiply op on new
  // since buffer is undefined, make sure you fill it completely!
  return new;
}
fluid relic
#

ummm

#

do i need to do something to access const pub fields?

native hound
#

keep both the comptime field and the pub const field

fluid relic
#

oh

native hound
#

or @TypeOf(self).Width if you just want one

#

the declarations will be part of the type while fields will be part of the instance

fluid relic
#

oh okay nice

#

they can have the same name as the fields

#

so i think thats fine to have it twice

native hound
#

similarily when you call method it's just syntax sugar for Type.foo(type, ...);

fluid relic
#

everything seems to work good

#

and now that i understand types better, i think i can figure out more

#

thanks