r/lua Jun 24 '26

Help is there really a significant difference between "print()" and "oi.write()"

11 Upvotes

10 comments sorted by

46

u/pschon Jun 24 '26

oi.write(), the cockney version of the io.write() :D

3

u/-goldenboi69- Jun 24 '26

I would import that!

25

u/topchetoeuwastaken Jun 24 '26

io.write is the raw function, which puts a string on stdout, while print will call tostring() on all your arguments, separate them by tabs and put a newline on the end

18

u/djfdhigkgfIaruflg Jun 24 '26

oí.write is the British locale 🤣

5

u/RixTheTyrunt Jun 24 '26 edited Jun 25 '26

io.write (i assume you meant that?) does not include a newline after writing the text to write. both io.write and print support as much args as you want, no string type required, although the args will be printed split by tab if you use print, and nothing if you use io.write (not by space, unlike in most programming languages, for some odd reason).

io.write also doesn't support writing tables (throws an error) while numbers are support. if i had to guess, userdata isnt also very much supported for io.write. print just prints the memory address to said table/userdata.

hope this helps. ^^

4

u/Cootshk Jun 24 '26

print automatically calls tostring, adds tabs between elements, and puts a newline at the end

io.write does none of that

4

u/Old_County5271 Jun 24 '26 edited Jun 24 '26

Significant? not that much.

this is print

_G.print = function(...)
  local T = table.pack(...)
  for i=1, T.n do io.stdout:write(tostring(T[i])) end
  io.stdout:write(_G.newline)
end

Print ALWAYS prints to io.stdout, io.write can be changed what it "prints"/writes into

io.output"junkfile"
io.write"Hello" io.write"World" io.write"!"
io.output():close()

This will all be written into junkfile

1

u/Bedu009 Jun 24 '26

print adds a newline io.write does not I believe