Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

casting? and "attempt to call value of type '((void) -> (any*) | nil)'" #12

Closed
daurnimator opened this issue Nov 17, 2014 · 11 comments
Closed

Comments

@daurnimator
Copy link

Trying to run:

interface foo
    unpack: (self) -> (number)
end
local t:foo = load("return {n=1; unpack = function(self) return self.n end}", nil, nil, nil)();
print(t:unpack())

Gives errors:

/dev/stdin:1:61: type error, attempt to call value of type '((void) -> (any*) | nil)'
/dev/stdin:1:53: type error, attempt to assign 'nil' to '{unpack:(self) -> (number)}'
/dev/stdin:1:149: type error, attempt to pass '(void)' to field of input type '(self)'

FWIW, I got this while trying to reproduce a different error as a test case.

@kevinclancy
Copy link

I think part of the problem is that the value load returns might be nil. If that's the case, then you can't call it. You probably need to check that its type or value is not nil before calling it, but I'm not sure how that is done in typed lua.

The example here might be relevant:
https://github.com/andremm/typedlua/blob/master/examples/typical/typical.tl

You should be able to do this:
local ret = load("return {n=1; unpack = function(self) return self.n end}", nil, nil, nil)
if ret then
local t:foo = ret()
print(t:unpack())
end

Does that work?

@andremm
Copy link
Owner

andremm commented Nov 17, 2014

Thanks for the reply Kevin. It is perfect.

andremm added a commit that referenced this issue Nov 17, 2014
@daurnimator
Copy link
Author

okay thanks. turns out this wasn't how to replicate my original issue.

Not sure if I should open a new issue, I'll try and ask here.
I was looking into providing type annotations for luatz.

As luatz/timetable.tld:

interface timetable
        year: number
        month: number
        day: number
        hour: number
        min: number
        sec: number
        yday: number
        wday: number

        unpack: (self) -> (number, number, number, number, number, number)
        normalise: (self) -> (self)
        timestamp: (self) -> (number)
        rfc_3339: (self) -> (string)
        strftime: (self, string) -> (string)
end

new: (number, number, number, number, number, number) -> (timetable)
new_from_timestamp: (number) -> (timetable)
cast: (any) -> (timetable)
local t:timetable = require"luatz.timetable".new_from_timestamp(123);
print(t:unpack())

I get the error:

/dev/stdin:1:77: type error, attempt to pass '(void)' to field of input type '(self)'

@andremm
Copy link
Owner

andremm commented Nov 17, 2014

I just pushed a commit that should fix this problem. Can you please pull it and test if it works?

@daurnimator
Copy link
Author

Looks like that fixed it!

@andremm
Copy link
Owner

andremm commented Nov 17, 2014

Cool! Thanks for the report.

@daurnimator
Copy link
Author

btw; is that the correct way to define a 'class'? the first items listed are actual contents; the later items are methods attached via __index.

And complete off topic now: do .tld files have a way to require/import other definitions?

@andremm
Copy link
Owner

andremm commented Nov 19, 2014

No, there is still not way to require/import other definitions inside .tld files.

Chapter 3 of my WIP PhD thesis includes a section about Object-Oriented programming with Typed Lua.
Did you check that?

@daurnimator
Copy link
Author

Thanks!

@daurnimator
Copy link
Author

No, there is still not way to require/import other definitions inside .tld files.

Is this still the case? Want me to open a new issue?

@andremm
Copy link
Owner

andremm commented Aug 13, 2015

Is this still the case? Want me to open a new issue?

Yes, this is still the case and please open an issue for this. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants