Code Monkey home page Code Monkey logo

Comments (7)

hishamhm avatar hishamhm commented on August 17, 2024

This is a little tricky. It allows declaring functions over unknown keys of records to support this:

local my_module = {}

function my_module.my_function()
   -- ...
end

...which is a super common pattern.

What you might be suggesting is that function t.unknown_key should work differently whether the table is declared in the same file or required from a different one (or perhaps it should be a matter of scope levels instead). In any case, it's an intriguing idea.

from tl.

pdesaulniers avatar pdesaulniers commented on August 17, 2024

Perhaps there should be an alternative record declaration syntax that forces you to declare all fields upfront? Something like this:

declare global love = record -- I'm not a fan of this 'declare' keyword... maybe you have a better idea?
	draw: function()
end

function love.draw() -- this works, since the `draw` key is known
	print("draw stuff...")
end

function love.draws() -- error: unknown key 'draws' in 'love'
	print("draw stuff...")
end

I think this feature would be useful for declaration files, since I don't think we typically want to "extend" the definitions outside of the initial record declaration.

from tl.

hishamhm avatar hishamhm commented on August 17, 2024

@pdesaulniers I made it in #48 so that it decides by scope whether it's ok or not to create a new function, the PR contains a description (and so do the tests), check it out!

from tl.

pdesaulniers avatar pdesaulniers commented on August 17, 2024

OK, that works!

I guess this behavior might seem a bit surprising for newcomers. Personally, I would expect global records to behave the same way as local records.

But then, I think it's fine for now. Global variables should generally be avoided anyway :)

from tl.

hishamhm avatar hishamhm commented on August 17, 2024

I agree, but then either love.draws would have to be accepted, or people would be forced to pre-declare every function in their module. At least I think I managed to produce a reasonable error message that explains the situation. At one point I'd like to do what the Rust compiler does and put "pointers" in the error messages such as error code numbers, that people can easily search online and reach an explanation that's longer than one line.

from tl.

pdesaulniers avatar pdesaulniers commented on August 17, 2024

Yep. This could be mentioned in the docs.

I think this behavior is fine for the time being.

from tl.

hishamhm avatar hishamhm commented on August 17, 2024

Yeah, will definitely document it... when I get around to start the documentation! Which should probably be the next thing for me to work on!

Going to merge the PR and close this one, thanks for bouncing ideas :)

from tl.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.