aardappel / lobster

The Lobster Programming Language
http://strlen.com/lobster
2.21k stars 117 forks source link

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

Closed NoidoDev closed 8 months ago

NoidoDev commented 8 months ago

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

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. 
aardappel commented 8 months ago

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.

NoidoDev commented 8 months ago

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)
aardappel commented 8 months ago

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 :)