aardappel/lobster

requires type `resource<texture>`, got `resource<texture>?`

NoidoDev opened this issue ยท 3 comments

I ran into some issues with following code. I hope I didn't just misunderstand something.

  • The program is supposed to switch the "tex" (texture) variable to the next image at a certain period of time. It seems to be so that I can only set it with let to be a constant. If I set "tex" with var then it creates following error later when I try to switch it.
  • The error message is:
    image.lobster(24): error: gl.set_primitive_texture (2nd argument) requires type resource<texture>, got resource<texture>?
    That message contradicts itself and doesn't tell me the problem. Also, I need a way to switch the image.
import gui
import texture

fatal(gl.window("Lobster", 1024, 1024))

var state = 0
let texlist = ["01.jpeg", "03.jpeg", "05.jpeg", "07.jpeg", "09.jpeg", "11.jpeg", "13.jpeg", >
var texpath = "/home/gamerone/Pictures/.../Renamed/" + pop(texlist)
var tex = gl.load_texture(texpath)
assert tex

def newtex():
    texpath = "/home/gamerone/Pictures/.../Renamed/" + pop(texlist)
    tex = gl.load_texture(texpath)
    assert tex

while gl.frame():
    if int(seconds_elapsed()) > state:
        newtex()
        state = int(seconds_elapsed())

    gl.clear(color_black)
    gl.set_primitive_texture(0, tex)
    ...
  • A way to bypass this might be create a batch of textures at the beginning and switch between them. But I wanted to mention the "weird" error.

the error message is as intended, and does not contradict itself.. it says the type of tex as passed in on that line is nillable, and it doesn't want that.

What is more curious is that it gets that type. What looks like that is happening is that the promotion by assert to be non-nil doesn't last outside newtex, which is kind of understandable.

To improve the code, do assert gl.load_texture(texpath), that way what gets assigned to tex is never ever nil to start with.
Alternatively, let it be nillable and do gl.set_primitive_texture(0, assert tex) when used. Both are better than the current assert locations which the type checker can't track.

Thanks. I have to learn more about certain concepts and understanding the type checker better, but this here works.

var texpath = "/home/gamerone/Pictures/.../Renamed/" + pop(texlist)
var tex = gl.load_texture(texpath)

def newtex():
    texpath = "/home/gamerone/Pictures/.../Renamed/" + pop(texlist)
    tex = gl.load_texture(texpath)
    return tex

while gl.frame():
    if int(seconds_elapsed()) > state:
        newtex()
        state = int(seconds_elapsed())

gl.clear(color_black)
gl.set_primitive_texture(0, assert tex)

It may seem like an annoyance at first, but I guarantee you the fact that it doesn't allow nil unchecked is an awesome feature :)