#is there a way to only specify uninferred types in generic functions?

33 messages · Page 1 of 1 (latest)

sonic oxide
#

I have this function

func daysIn[m Integer, y IntegerYear, r Integer](month m, year y) r {
    if month == 2 && isLeapYear(year) {
        return 29
    }
    return r(daysInMonth[month])
}

and in some cases when using daysIn, I get the compiler error that r cannot be inferred. In this case specifying all types like this

if days > daysIn[int, int, int](m, y) {}

resolves the issue, but is it possible to only specify r in those cases?
is there something like this maybe?

if days > daysIn[r: int](m, y) {}
#

is there a way to only specifyuninferred types in generic functions?

#

is there a way to only specify uninferred types in generic functions?

civic crescent
#

m and r are the same, why not just merge them? And you don't have to specify the type at all.

sonic oxide
#

r can be different as well

#

the underlying daysInMonth is uint8 though

#

sometimes i need to

if days > daysIn[uint8, uint16, int](m, y) {}
#

idk know why the return type can't be inferred since the type of days should be obvious

civic crescent
#

You can either specify all the types or not at all.
In your case, you need to specify all the types. If not, what would be the type ofr ?

sonic oxide
#

same type as days, no?

civic crescent
#

You said that month and days (m and r), could be different types.

sonic oxide
#

i mean this one if days

civic crescent
#

Oh, you mean that the compiler should be able to infer the type based on the expression.
Just like if int64(5) > 6 the 6 implicitly become an int64

#

hm...

sonic oxide
#

yes

#

the compiler knows the type of days and it knows that r can be any integer type, so why can't it make r the same type as days?

fair forge
#

Partial inference is only available from left to right. You should probably move r Interger to the left

sonic oxide
#

@fair forge
neither

func daysIn[ret Integer, m Integer, y IntegerYear](month m, year y) ret {

this, nor

if daysIn(m, y) <= days {}

this works

#

doing both doesnt work either

civic crescent
sonic oxide
#

i see

#

so it's probably coming in the future lol

#

thx guys

#

how do i mark this solved?

civic crescent
#

yah, go can't infer type from generic interface either.

fair forge
#

m & y will be inferred

sonic oxide
#

oh nice

#

yea that works

#

the if can be in either order

#

there's even fancy warnings

fair forge
#

it tells you to remove them and let the compile infer them?

sonic oxide
#

yep