Search code examples
goglobal-variablesfirst-class-functions

Go, first class functions, and best practices


For the past few days, I've been at a road block with regards to the best way to approach the issue of first class functions (assigning a callable to some variable), and the best practices in terms of efficiency.

Let's say I am programming a Yugioh card game, and I want each individual card of type card to have at least these attributes:

type card struct {
    name string
    text string
}

I have been struggling with the idea on where (and how) to program each card's individual functionality. I am currently convinced that the best place for the first-class function is in type card struct and create the new attribute as a "callable" like I would in Python (Go playground link).

package main

import "fmt"


type card struct {
    name string
    text string
    f interface{}
}

type monsterCard struct {
    card
    attack int
    defense int
}

type buff func(target *monsterCard) // Could be defined in a second file

type swap func(target *monsterCard, value int) // ditto

var increaseAttack buff = func(target *monsterCard)  { // ditto
    target.attack += 100
}

var swichStats swap = func(target *monsterCard, value int) { // ditto
    attack := target.attack
    target.attack = value
    target.defense = attack
}

func main()  {
    m1 := monsterCard{
        card:    card{
            name: "Celtic Guardian",
            f:    increaseAttack,
        },
        attack:  1400,
        defense: 1200,
    }
    m2 := monsterCard{
        card:    card{
            name:     "Dark Magician",
            f:         swichStats,
        },
        attack:  2500,
        defense: 2100,
    }
    var monsters = [2]monsterCard{m1, m2}
    for _, m := range monsters {
        fmt.Println(m)
        switch m.f.(type) {
        case buff:
            m.f.(buff)(&m)
        case swap:
            m.f.(swap)(&m, m.defense)
        default:
            fmt.Printf("%T", m.f)
        }
        fmt.Println(m)
    }
}

I'm not very good with regards to efficient code, and I completely understand I might be optimizing early here; however, I will need to program hundreds of these cards, and if having these callables exist in global scope with a heavy reliance on type assertion make the program slow, then I'll be in trouble reorganizing the code.

Are there any glaring issues that you can see with my methodology? Am I going about first-class functions correctly, or is there some kind of glaring performance issues I can't see? Any and all help will be greatly appreciated!


Solution

  • You can use plain functions, and closures where needed:

    type card struct {
        name string
        text string
        fn   func(*monsterCard)
    }
    
    type monsterCard struct {
        card
        attack  int
        defense int
    }
    
    func (mc *monsterCard) call() {
        mc.fn(mc)
    }
    
    func increaseAttack(mc *monsterCard) {
        mc.attack += 100
    }
    
    func switchStats(mc *monsterCard) {
        mc.attack, mc.defense = mc.defense, mc.attack
    }
    
    
    func updateTextAndAttack(text string, attack int) func(mc *monsterCard) {
        return func(mc *monsterCard) {
            mc.text, mc.attack = text, attack
        }
    }
    

    https://play.golang.com/p/v_RbObnu7sN

    You can also use plain methods, and closures where needed:

    type card struct {
        name string
        text string
    }
    
    type monsterCard struct {
        card
        attack  int
        defense int
    }
    
    func (mc *monsterCard) increaseAttack() {
        mc.attack += 100
    }
    
    func (mc *monsterCard) switchStats() {
        mc.attack, mc.defense = mc.defense, mc.attack
    }
    
    func (mc *monsterCard) updateTextAndAttack(text string, attack int) func() {
        return func() {
            mc.text, mc.attack = text, attack
        }
    }
    

    https://play.golang.com/p/Eo1mm-seldA