luau icon indicating copy to clipboard operation
luau copied to clipboard

type userdatas in type functions should have a __tostring metamethod

Open gaymeowing opened this issue 10 months ago • 3 comments

Currently there is no way to nicely provide types in errors in type functions, while I could make my own type to string function and copy it around everywhere its very cumbersome to do so. And also makes type resolution take more time, when luau already has a string version of the type.

Current Behavior:

TypeError: 'LeaderstatsCreator' type function errored at runtime: 
[string "LeaderstatsCreator"]:55: value 'userdata: 0x0000000102864718' for key 'userdata: 0x0000000102864730' 
is not of type 'string | number | buffer' or is a type union made up of 'string | number | buffer'

New Behavior:

TypeError: 'LeaderstatsCreator' type function errored at runtime: 
[string "LeaderstatsCreator"]:55: value '"buffer"' for key '"lol"' 
is not of type 'string | number | buffer' or is a type union made up of 'string | number | buffer'

gaymeowing avatar Feb 16 '25 17:02 gaymeowing

Could this be formatted in the usual tostring(someuserdata) fashion with angle brackets, like:

type function printtype(t: type)
    print(t)
    return types.singleton(nil)
end

local a: printtype<"meow"> -- TypeError: <type: singleton string "meow">

type SomeStruct = {
    cats: {
        [string]: { name: string, age: number },
    },
}

local somet: printtype<SomeStruct> -- TypeError: <type: table SomeStruct { cats: { [string]: { name: string, age: number } } }>

This would make type functions easier to print debug and reason through

deviaze avatar Feb 16 '25 18:02 deviaze

Could this be formatted in the usual tostring(someuserdata) fashion with angle brackets, like:

type function printtype(t: type) print(t) return types.singleton(nil) end

local a: printtype<"meow"> -- TypeError: <type: singleton string "meow">

type SomeStruct = { cats: { [string]: { name: string, age: number }, }, }

local somet: printtype<SomeStruct> -- TypeError: <type: table SomeStruct { cats: { [string]: { name: string, age: number } } }>

This would make type functions easier to print debug and reason through

This issue is meant to match with how luau's type errors are formatted. Angle brackets aren't used in luau's type errors and instead single quotation marks are ''.

gaymeowing avatar Feb 16 '25 18:02 gaymeowing

Could this be formatted in the usual tostring(someuserdata) fashion with angle brackets, like: type function printtype(t: type) print(t) return types.singleton(nil) end local a: printtype<"meow"> -- TypeError: <type: singleton string "meow"> type SomeStruct = { cats: { [string]: { name: string, age: number }, }, } local somet: printtype -- TypeError: <type: table SomeStruct { cats: { [string]: { name: string, age: number } } }> This would make type functions easier to print debug and reason through

This issue is meant to match with how luau's type errors are formatted. Angle brackets aren't used in luau's type errors and instead single quotation marks are ''.

That makes sense! I guess I was referring to when you print type userdatas directly.

deviaze avatar Feb 16 '25 19:02 deviaze