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

disallow extending records with functions outside of original scope #48

Merged
merged 1 commit into from
Feb 6, 2020

Conversation

hishamhm
Copy link
Member

@hishamhm hishamhm commented Feb 5, 2020

tl supports the common pattern of adding functions to an empty table without having to declare them beforehand, which is used in module declarations:

local foo = {}

function foo.fun()
end

A side effect to this was #39, where arbitrary functions could be created by callers of a module, when making a typo while trying to redefine a function (e.g. for a callback, like love.draw).

This restricts the definition of undeclared functions to the same scope level where a record is declared only. So the above example works, but trying to create new functions from other files don't. to declare functions from other files, you have to declare a record type and declare the function as a field to that record, like this:

-- love.d.tl
global love = record
   draw: function()
end
-- main.tl
function love.draw() -- this works
end

function love.bla() -- this doesn't
end

Restricting it by scope might be overly restrictive (a simple wrapper do block will cause a function to be in a different scope and be rejected) and an alternative to consider could be to do it by file, but there's always the alternative of forward-declaring functions first (like tl itself does for tl.process and tl.type_check for example in lines 2 and 3), so this limitation shouldn't be a show-stopper. So I think this behavior should be fine for now.

Fixes #39.

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

Successfully merging this pull request may close these issues.

Method definition on record imported from declaration file does not throw an error?
1 participant