teal-language / tl

The compiler for Teal, a typed dialect of Lua
MIT License
2.11k stars 109 forks source link

"not all enum values map to record fields of the same type" message proposal. #514

Closed nagolove closed 1 year ago

nagolove commented 2 years ago

I have some code like this:

local enum Command
    "circle"
    "line"
    "rect"
end

local commands = {}

function commands.circle(_: string)
end

function commands.line(_: number)
end

function commands.rect(_: integer)
end

local cmd: Command = 'circle'
commands[cmd]()

And I get such message from compiler:

1 error:
example.tl:19:10: cannot index, not all enum values map to record fields of the same type

I have stumbled for time because of did not attended to function types. My idea is to write more complex message like:

example.tl:19:10: cannot index, not all enum values map to record fields of the same type( function(string) ~= function(number) )

In this message I have direct indication for first incompatible types pair. Implementation proposal: apply text templates in match_all_record_field_names function argument like "cannot index, not all enum values map to record fields of the same type( <f> ~= <t>)" and replacing placeholders with real type names usingstring.gsub() .

hishamhm commented 2 years ago

I suspect the errors could get pretty longwinded if they print the entire function signatures. Perhaps using their names, such as (types of fields 'circle' and 'line' do not match)?

nagolove commented 2 years ago

@hishamhm Yes, only names are enough in most cases.

hishamhm commented 2 years ago

Patches are welcome! :grin: