Skip to content

Order of type declarations #75

@BrownNPC

Description

@BrownNPC

According to the spec:

There's no automatic order for declarations within a package. You need to declare constants, variables, and types in the order that C expects:

If a function F uses a constant C or a variable V, you must declare V and C before F.
If type B refers to type A, you must declare A before B.

The issue:
suppose you have 2 files in a package

game/game.go

package game

import "mbc/gfx"


// Game state
type State struct {
	Dt float32

	Pack TexturePack
}
func (s *State) Init() {
	s.Pack = NewDefaultTexturePack()
}

// return false to quit.
func (s *State) Update() bool {

	gfx.BeginDrawing()
	gfx.ClearBackground(gfx.Red)
	packPNG := s.Pack.GetTexture("/pack.png")
	gfx.DrawTexture(packPNG, 0, 0, 1)
	gfx.EndDrawing()
	return true
}

game/types.go

package game

import (
	"mbc/gfx"

	"solod.dev/so/maps"
	"solod.dev/so/mem"
)

type TexturePack interface {
	Icon() gfx.Texture // should always return a valid texture.
	Name() string // name of the pack
	Description() string // Description of the pack
	GetTexture(path string) gfx.Texture // will return zero value if not found.
	Unload() // free all textures and memory.
}

type DefaultTexturePack struct {
	Textures maps.Map[string, gfx.Texture]
	scratch  mem.Arena
}

Currently this package will not compile because State references TexturePack and TexurePack just so happens to be declared after State in the compiled source.

While I could just declare state inside of game/types.go having it inside of game/game.go next to all its methods is better because A: you can modify state without jumping between files, and B: you can see the state without jumping files.

I propose some way to "sort" the order in which files are transpiled.

something like a //so:priority 0 pragma.

By default every file would have a priority of 0. But you can place so:priority 1 and that would cause the file to be processed AFTER all the files with priority 0.

Would such feature be easy to implement? I suppose you can do a pass on the collected files and sort them before producing the output.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions