Skip to content

docs: remove unnecessary and true in example config#73

Open
herrscher-of-sleeping wants to merge 1 commit intofolke:mainfrom
herrscher-of-sleeping:fix_logic_expression_in_readme_example
Open

docs: remove unnecessary and true in example config#73
herrscher-of-sleeping wants to merge 1 commit intofolke:mainfrom
herrscher-of-sleeping:fix_logic_expression_in_readme_example

Conversation

@herrscher-of-sleeping
Copy link
Copy Markdown

Description

Just a small cleanup in example config. and true does nothing unless I'm missing something here.

Related Issue(s)

None

Screenshots

None

@github-actions
Copy link
Copy Markdown
Contributor

This PR is stale because it has been open 30 days with no activity.

@github-actions github-actions Bot added the stale This issue or PR has been inactive for a while label Nov 16, 2024
Copy link
Copy Markdown
Contributor

@DrKJeff16 DrKJeff16 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@herrscher-of-sleeping
That and true is a logical expression to give a default value if vim.g.lazydev_enabled is not explicitly set.
In a nutshell, this statement:

return vim.g.lazydev_enabled == nil and true or vim.g.lazydev_enabled

is IDENTICAL to the following statement:

if vim.g.lazydev_enabled == nil then -- if variable is nil
    return true -- Return default value (`true`)
end

-- Otherwise, return the value (whether `true` or `false`) of `vim.g.lazydev_enabled`
return vim.g.lazydev_enabled 

Essentially this is just a safeguard to enable the plugin unless explicitly said not to.

The Lua reference is here

@herrscher-of-sleeping
Copy link
Copy Markdown
Author

herrscher-of-sleeping commented Jul 17, 2025

Maybe I'm missing something here, but still let me explain.

There's no difference between vim.g.lazydev_enabled == nil and true and vim.g.lazydev_enabled == nil, other than extra code. It's not a ternary operator-like thing (Lua doesn't have such, and x and y or z put in its place tends to lead to logical mistakes) and it doesn't have to look like one.

vim.g.lazydev_enabled == nil evaluates to either false or true, and if it's:

  1. false, then vim.g.lazydev_enabled == nil and true will also be false, because false and true is false. The whole expression then evaluates to whatever vim.g.lazydev_enabled is.
  2. true, then vim.g.lazydev_enabled == nil and true is also true, and the whole expression evaluates to true.

Hence there's no point in adding and true.

I also prepared an example script that compares result of all the 3 versions of this calculation for all possible inputs that make sense to be tested:

  1. nil (falsey value)
  2. false (also falsey value, but is used when we explicitly set vim.g.lazydev_enabled to false)
  3. true (obviously truthy)
  4. "kutulu" (truthy value, any other value in Lua is truthy and the expression will work the same way)
local function calc0(lazydev_enabled)
  if lazydev_enabled == nil then
      return true
  end

  return lazydev_enabled
end

local function calc1(lazydev_enabled)
  return lazydev_enabled == nil and true or lazydev_enabled
end

local function calc2(lazydev_enabled)
  return lazydev_enabled == nil or lazydev_enabled
end

local input_values = {
  nil,
  false,
  true,
  "kutulu",
}

for i = 1, 4 do
  local value = input_values[i]
  print(("result for %6s is: %6s, %6s, %6s"):format(value, calc0(value), calc1(value), calc2(value)))
end

All the three functions return the same value:

result for    nil is:   true,   true,   true
result for  false is:  false,  false,  false
result for   true is:   true,   true,   true
result for kutulu is: kutulu, kutulu, kutulu

@github-actions github-actions Bot removed the stale This issue or PR has been inactive for a while label Jul 30, 2025
@github-actions
Copy link
Copy Markdown
Contributor

This PR is stale because it has been open 30 days with no activity.

@github-actions github-actions Bot added the stale This issue or PR has been inactive for a while label Aug 30, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

stale This issue or PR has been inactive for a while

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants