Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Go - Idiomatic way around interface slice issue

Tags:

go

I have a WorkDay struct holding data about the times someone worked, a WorkWeek struct to hold a bunch of WorkDays, and a WorkMonth struct to hold a bunch of WorkWeeks. The idea is to have each return the total hours worked during that period.

type WorkDay struct {
    StartTime time.Time
    EndTime   time.Time
}

type WorkWeek struct {
    WorkDays []WorkDay
}

type WorkMonth struct {
    WorkWeeks []WorkWeek
}

func (w WorkDay) HoursWorked() time.Duration {
    // Find hours worked through simple subtraction.
}

func (w WorkWeek) HoursWorked() time.Duration {
    var totalHours time.Duration
    for _, day := range w.WorkDays {
        totalHours += day.HoursWorked()
    }
    return totalHours
}

func (w WorkMonth) HoursWorked() time.Duration {
    var totalHours time.Duration
    for _, week := range w.WorkWeeks {
        totalHours += week.HoursWorked()
    }
    return totalHours
}

This code works just fine, but the duplication in WorkWeek.HoursWorked() and WorkMonth.HoursWorked() really grinds my gears. I tried to do the following, thinking I was very clever:

func (w WorkWeek) HoursWorked() time.Duration {
    return sumHoursWorked(w.WorkDays)
}

func (m WorkMonth) HoursWorked() time.Duration {
    return sumHoursWorked(m.WorkWeeks)
}

type countable interface {
    HoursWorked() time.Duration
}

func sumHoursWorked(timeFrames []countable) time.Duration {
    var totalHours time.Duration
    for _, frame := range timeFrames {
        totalHours += frame.HoursWorked()
    }
    return totalHours
}

However, as explained here, even though WorkDay implements countable, a slice of WorkDays does not count as a slice of countables.

So, is there some nifty, idiomatic way out of this situation that I'm missing, or am I just stuck with the duplication?

like image 403
sozorogami Avatar asked May 22 '14 10:05

sozorogami


2 Answers

No. Either have a slice of countables which gives you dynamic method dispatch or some programming (your first solution) or restructure your types. I have no idea of your problem domain but months consisting of weeks consisting of days seems odd, at least the month/week stuff.

like image 175
Volker Avatar answered Sep 27 '22 18:09

Volker


No because slice of countables is another type. You could define your own slice type and attach an Add method to it.

func (w WorkWeek) HoursWorked() time.Duration {
    return sumHoursWorked(w.WorkDays)
}

func (m WorkMonth) HoursWorked() time.Duration {
    return sumHoursWorked(m.WorkWeeks)
}

type countable interface {
    HoursWorked() time.Duration
}

type SliceCountable []countable

func (m *SliceCountable) Add( c countable ) {
   *m = append(*m, c ) 
}

func (m SliceCountable) HoursWorked() time.Duration {
var totalHours time.Duration
for _, frame := range m {
    totalHours += frame.HoursWorked()
}
return totalHours
}


func sumHoursWorked(timeFrames []countable) time.Duration {
    var totalHours time.Duration
    for _, frame := range timeFrames {
        totalHours += frame.HoursWorked()
    }
    return totalHours
}
like image 36
fabrizioM Avatar answered Sep 27 '22 19:09

fabrizioM