Skip to content

Question about polymorphic function and undeclared symbol #99

Answered by edubart
codehz asked this question in Q&A
Discussion options

You must be logged in to vote

The problem with your first example is that the symbol is not visible due to declaration order, your workaround is correct, saving the value.type to the local TargetType makes the symbol always visible inside printEnum.

Congrats on being able to make some interesting function with meta programming!

Another possible implementation of this is the following:

local enumType = #[concept(function(attr)
  return attr.type.is_enum
end)]#

global function printEnum(value: enumType): string
  ## for i,field in ipairs(value.type.fields) do
  if value == #[field.value]# then
    return #[field.name]#
  end
  ## end
  return 'Unknown'
end

-- it doesn't work here
local Boom = @enum{ A = 0 }
print(prin…

Replies: 2 comments

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Answer selected by edubart
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants