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

Provide a description-scope api to determine whether an element is inside a table #1774

Closed
xq114 opened this issue Oct 26, 2021 · 4 comments
Closed

Comments

@xq114
Copy link
Contributor

xq114 commented Oct 26, 2021

你在什么场景下需要该功能?

目前想要判断一个列表中是否含有某元素,在script scope只需要

local var = import("core.base.hashset").of(mytable):has(myelement)

但在description scope需要

local var = false
for _, v in ipairs(mytable) do
  if v == element then
    var = true
    break
  end
end

相当麻烦。lua本身也没有提供一种这样的机制,见 https://stackoverflow.com/questions/2282444/how-to-check-if-a-table-contains-an-element-in-lua 。对比之下,cmake只需要一行

set(VAR (myelement IN_LIST mytable))

这个操作还是挺常见的,既然lua没有提供,xmake能否提供一个简单通用的api来判断列表对元素的包含关系?

描述可能的解决方案

table.contain(mytable, myelement) -- return true / false

或者

mytable:has(myelement)

描述你认为的候选方案

让hashset在description scope也能用

@waruqi waruqi added this to the v2.5.9 milestone Oct 26, 2021
@waruqi
Copy link
Member

waruqi commented Oct 26, 2021

加了个 table.contains(t, v)

@waruqi waruqi closed this as completed Oct 26, 2021
@waruqi
Copy link
Member

waruqi commented Oct 27, 2021

改进了下 table.contains(t, 1, 2, 3, ...) 包含其中一个就返回 true

@xq114
Copy link
Contributor Author

xq114 commented Oct 28, 2021

改进了下 table.contains(t, 1, 2, 3, ...) 包含其中一个就返回 true

这样的接口感觉不如table.contains(t, {1, 2, 3, ...})实用,后者还能表达两个table的包含关系。不过应该也是等价的,现在可以写

table.contains(t, unpack(subset))

@waruqi
Copy link
Member

waruqi commented Oct 28, 2021

但写起来不省事,也跟 is_plat has_config 这种风格不一致,并且还会每次new 一个额外的table

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

No branches or pull requests

2 participants