Interface return type not enforced if inferred in other function #805

Closed
davidhesselbom opened this Issue Aug 19, 2014 · 3 comments

Projects

None yet

3 participants

@davidhesselbom
Contributor
import math

IColor: interface {
    set: func (color: IColor)
    asMonochrome: func -> ColorMonochrome
    asBgr: func -> ColorBgr
    copy: func -> IColor
}

ColorMonochrome: cover implements IColor {
    y: UInt8
    set: func (color: IColor) {
        this y = color asMonochrome() y
    }
    init: func@ (=y)
    copy: func -> ColorMonochrome { This new(this y) }
    asMonochrome: func -> This { this copy() }
    asBgr: func -> This { ColorConvert monochromeToBgr(this) }
}

ColorBgr: cover implements IColor {
    blue, green, red: UInt8
    set: func (color: IColor) {
        bgrColor := color asBgr()
        this blue = bgrColor blue
        this green = bgrColor green
        this red = bgrColor red
    }
    init: func@ (=blue, =green, =red)
    copy: func -> This { This new(this blue, this green, this red) }
    asMonochrome: func -> ColorMonochrome { ColorConvert bgrToMonochrome(this) }
    asBgr: func -> This { this copy() }
}


ColorConvert: cover {
    monochromeToBgr: static func (color: ColorMonochrome) {
        ColorBgr new(0, 0, 0)
    }
    bgrToMonochrome: static func (color: ColorBgr) {
        ColorMonochrome new(0)
    }
}

mono := ColorMonochrome new(0)
bgr := mono asBgr()
"#{bgr y}" println() //This shouldn't work, but it does
"#{bgr blue}, #{bgr green}, #{bgr red}" println() //Comment this out, and it builds, but shouldn't

bgr here is a ColorMonochrome because that's what ColorMonochrome asBgr() returns. But the interface IColor says that asBgr() must return a ColorBgr, so ColorMonochrome shouldn't build!

If ColorConvert monochromeToBgr() is given a return type -> ColorBgr, however, ColorMonochrome does not build until you change the return type for ColorMonochrome asBgr() to ColorBgr, but only then. What's going on here?

@davidhesselbom davidhesselbom changed the title from Interface return type not enforced if inferred to Interface return type not enforced if inferred in other function Aug 19, 2014
@shamanas
Collaborator

Sounds like ColorBgr asBgr() is not fully resolved, unless you reference it from ColorCovert or something.

I'll find a small testcase and check this out.

@shamanas
Collaborator

Ok so apparently when an interface's function is implemented, its signature is not matched against the one that is provided from the interface.

However, I actually can't understand why an error is raised when the return type is added.

@shamanas
Collaborator

I will be adding a scoring mechanism for a fDecl vs a fDecl (really similar to FunctionCall getScore()), I bet it will be useful in other places too ;)

@shamanas shamanas closed this in #807 Aug 19, 2014
@fasterthanlime fasterthanlime modified the milestone: 0.9.10 Jul 10, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment