aardappel / lobster

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

compiler / type system bug: constructor` (1st argument) requires type X, got X #182

Closed DerTee closed 2 years ago

DerTee commented 2 years ago

I get the following error with the code below:

bug_test.lobster(10): error: `constructor` (1st argument) requires type `configuration`, got `configuration`
  in bug_test.lobster(1): run() { }, context:undefined

The code:

run() // this call produces the error

class configuration:
    resolution_x:int = 1024
    resolution_y:int = 768

class context:
    config:configuration = configuration {}

def run():
    let context = context { }
    print("Resolution: {context.config.resolution_x}, {context.config.resolution_y}")

// run() // this one works 

I'm very unclear about this bug, but it happens reliably for and it's a problem dependent on two things:

aardappel commented 2 years ago

Thanks! I'll have a look :)

aardappel commented 2 years ago

Fixed in https://github.com/aardappel/lobster/commit/a793841bb7cc3f997b94ad7a3608b394e478f043 (which hopefully fixes many similar bugs, and will make future ones more obvious/fixable).

In general, not all possible combinations of ordering will work with type inference, "define before use" as much as possible avoids errors (if configuration or context are changed to have generic parameters, it will still not work). But with this fix it should do a little better.