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

Optional Compiler-enforced nil-checking #721

Closed
paulsgcross opened this issue Nov 11, 2023 · 2 comments
Closed

Optional Compiler-enforced nil-checking #721

paulsgcross opened this issue Nov 11, 2023 · 2 comments
Labels
duplicate This issue or pull request already exists feature request New feature or request

Comments

@paulsgcross
Copy link

paulsgcross commented Nov 11, 2023

Apologies if this is already a feature.

Context

Currently, it is possible to do this:

local record MyRecord
     SomeField: number
end

local function MyFunction(rec : MyRecord)
     -- Do something...
end

MyFunction({ }) -- This is what I am interested in.

This is fine and supported by the Lua syntax (it is equivilant to 'assigning' nil to every field) and so no reason to prevent it. However, there would be benefit to optionally enforce that specific (or all) Record fields must not be 'nil' when passed into a function or declared as a type - compiler-side.

Proposal

When defining records, allow the programmer to set fields as <required> (a different name to distinguish from require would be better):

local record MyRecord
     <required> SomeField: number
end

-- Compiler will Complain:
local variable : MyRecord = {} 
MyFunction({ })

-- Compiler will Be Happy:
local variable : MyRecord = {SomeField = 5} 
MyFunction({SomeField = 5})

Likewise, if you're lazy and don't want to be forced to set this for every field:

local <required> record MyBigRecord
     SomeField: number
     AnotherField: number
     PleaseStopAddingFields: number
end

MyFunction({SomeField = 5, AnotherField = 4}) -- Compiler will complain.
MyFunction({SomeField = 5, AnotherField = 4, PleaseStopAddingFields = 1}) -- Compiler is happy.

This would be the equivilent of adding <required> to every field.

Anything that matches the required record signature should also be accepted as to allow for polymorphism and the ability to substitute records with 'sub-types':

local <required> record MyBigRecord
     SomeField: number
end

local record AnotherRecord
     SomeField: number
end

local RecOne: AnotherRecord = {}
local RecTwo: AnotherRecord = {SomeField = 5}

MyFunction(RecOne) -- Compiler will complain.
MyFunction(RecTwo) -- Compiler is happy.

Pros

  • Allows the enforcement of interfaces without sacrificing Lua-ness;
  • Favours composition over inheritence and avoids the can-of-worms in realising a full class model;
  • Essentially compile-time nil-checking;
  • No additional lua output is generated;

Cons

  • Additional, potentially niche, language feature that takes time to implement;
  • Terrible name, maybe "enforced" is better;
  • Probably others, may prevent the "required" records being used in a specific way that sacrifices lua-ness;
@paulsgcross paulsgcross changed the title Proposal: Proposal: Compiler-enforced nil checking Nov 11, 2023
@paulsgcross paulsgcross changed the title Proposal: Compiler-enforced nil checking Proposal: Optional Compiler-enforced nil-checking Nov 11, 2023
@hishamhm hishamhm changed the title Proposal: Optional Compiler-enforced nil-checking Optional Compiler-enforced nil-checking Nov 13, 2023
@hishamhm hishamhm added feature request New feature or request duplicate This issue or pull request already exists labels Nov 13, 2023
@hishamhm
Copy link
Member

hishamhm commented Nov 13, 2023

Nil-checking has already been asked for several times. Please see #598 and following discussions. Closing this one as a duplicate, thanks for understanding!

@hishamhm hishamhm closed this as not planned Won't fix, can't repro, duplicate, stale Nov 13, 2023
@paulsgcross
Copy link
Author

Yep, totally understand. Not sure if I added much to the discussion but will continue to funnel any thoughts into those open issues :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
duplicate This issue or pull request already exists feature request New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants