teal-language / tl

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

Circular imports causes no type information for required module error check #573

Closed WeebNetsu closed 1 year ago

WeebNetsu commented 1 year ago

I have 2 files, player.d.tl

require "src.game.deck"
require "src.game.card"
require "src.game.com"

-- player has all the same types as Com
-- Com inherits from the player type
global record PlayerType
    playerTurn: boolean

    setPlayerTurn: function (PlayerType, playerTurn: boolean)
    showPlayableCards: function (PlayerType)
    -- to be used to see if a card is being hovered over, return true or false
    checkHover: function (PlayerType): boolean
    -- used to see which card is being hoved over, table of booleans is returned
    checkHovering: function (self: PlayerType): {boolean}

    -- com also has one of these functions, don't also put them into this file
    updateCardPositions: function (PlayerType)
    removeCard: function (PlayerType, index: integer, com: ComType)
    draw: function (PlayerType)

    -- com + player stuff
    difficulty: integer
    defaultXPos: number
    defaultYPos: number
    cards: {CardType}
    skipTurn: boolean
    saidUno: boolean

    -- will set the initial 8 cards
    -- num - number of cards to draw
    setCards: function (ComType, num: number)
    addCard: function (ComType, card: CardType)
    getCard: function (ComType, index: integer): CardType
    drawCard: function (ComType): any
    play: function (ComType, player: any)
end

global PlayerDeckFunc: function(deck: DeckType, playedDeck: PlayedDeckType): PlayerType

return PlayerDeckFunc

and com.d.tl

require "src.game.card"
require "src.game.deck"
require "src.game.player"
require "src.game.played_deck"

global record ComType
    difficulty: integer
    defaultXPos: number
    defaultYPos: number
    cards: {CardType}
    skipTurn: boolean
    saidUno: boolean

    -- will set the initial 8 cards
    -- num - number of cards to draw
    setCards: function (ComType, num: number)
    updateCardPositions: function (ComType)
    addCard: function (ComType, card: CardType)
    removeCard: function (ComType, index: number)
    getCard: function (ComType, index: integer): CardType
    drawCard: function (ComType): any
    draw: function (ComType)
    play: function (ComType, player: PlayerType)
end

global ComFunc: function(deck: DeckType, playedDeck: PlayedDeckType): ComType

return ComFunc

As you can see, both the ComType and PlayerType require each other in some way... However, this causes a no type information for required module error check error to pop up, since com.d.tl cannot import player.d.tl if player.d.tl is already importing com.d.tl.

It would be nice if circular imports/circular dependency was allowed, especially in projects using Love2D, this feature will be very much appreciated

hishamhm commented 1 year ago

You can use a global type forward declaration as described here: https://github.com/teal-language/tl/issues/355#issuecomment-1208065660

Try adding global type ComType at the top of player.d.tl and global type PlayerType at the top of com.d.tl!

WeebNetsu commented 1 year ago

Yes! This works! Thank you!