#TypeChecker or sum idk what that is called

1 messages · Page 1 of 1 (latest)

wooden valley
#
--!strict

export type Core = {
    DEBUG: boolean,
    _VERSION: string,
    func : any
}

local core : Core = {
    DEBUG = false,
    _VERSION = "0.1"
}

function core.func()
    print("test")
end

return core

is this legitim, I dont really understand the use of this but im trying to figure it out.

#

I wanted to declaire functions outside of the module so I have 1 nest less and this is the only possible way it seems to work

snow bison
#

why would u need strict or export type

#

if u just want to have function accessable outside module

wooden valley
#

im trying to learn strict and export type rnevilcat

snow bison
#

wait what r u exacly trying to do

wooden valley
#
--!strict

export type Core = {
    DEBUG: boolean,
    _VERSION: string,
    func : (self: Core) -> ()
}

local core : Core = {
    DEBUG = false,
    _VERSION = "0.1",
    
    func = function() -- I dont want this here
        print("test")
    end
}

return core

Maybe this explains it better

#

But if I give func the Type any I am able to make it nil and then "declaire" it outside of the table

#
--!strict

export type Core = {
    DEBUG: boolean,
    _VERSION: string,
    func : (self : Core) -> ()
}

local core : Core = {
    DEBUG = false,
    _VERSION = "0.1",
    func = function(self : Core) end
}

core.func = function(self : Core) -- This is fine
    
end

return core

like this it would also work and I wouldn't use any

sour ravine
#

if i may weigh in on this, given i've worked a good amount with metatables, oop and typechecking:
first - https://luau.org/typecheck is a gold mine of info. it will serve you very well if you read it in full.

for this situation, changing func to : any will not help as it will continue to remain any outside the module. what this would require is for the module table to be unsealed after declaration, which is not possible after you leave the scope.

but i have a bit of a pattern i found.

wooden valley
#

"would require is for the module table to be unsealed after declaration, which is not possible after you leave the scope"

I didn't understand this at all btw

sour ravine
#

i see

#

ill see if i understand the problem correctly first

#

you want to declare functions outside of the module

wooden valley
#

yea outside of the table

#

which is the module ig

sour ravine
#

ok. basically this is not normally possible

#

because outside of the module, the table is "sealed"

#

meaning the type channot change.

wooden valley
#

no

#

I think you are missunderstanding the problem

sour ravine
#

i think i am

sour ravine
#

i still don't exactly follow

#

wait

#

ok i think i see

wooden valley
#
export type Core = {
    DEBUG: boolean,
    _VERSION: string,
    func : (self : Core) -> ()
}

local core : Core = {
  DEBUG: boolean,
  _VERSION: string,
  -- I dont want the function to be declaired in here
}

-- But here

return core
#

Theres not really a problem here

sour ravine
#

yea that makes sense now

wooden valley
#

I just wanna know if it is legitim todo func : any and declaire it outside

sour ravine
#

got it.
in that case func : any is technically legitimate but will not allow further typechecking (it will always result in any)
the second method is better.

wooden valley
#

alright

#

ima use the 2nd then thanks

sour ravine
#

no problem, sorry for the flip-flopping back there

wooden valley
#

all good