New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

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

Comments

Projects
None yet
3 participants
@davidhesselbom
Contributor

davidhesselbom commented Aug 19, 2014

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

@alexnask

This comment has been minimized.

Collaborator

alexnask commented Aug 19, 2014

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.

@alexnask

This comment has been minimized.

Collaborator

alexnask commented Aug 19, 2014

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.

@alexnask

This comment has been minimized.

Collaborator

alexnask commented Aug 19, 2014

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 ;)

alexnask added a commit that referenced this issue Aug 19, 2014

Merge pull request #807 from shamanas/master
Added scoring mechanism between function declarations, use is to check implemented functions for correctness

Closes #805

@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