rafaqz / Interfaces.jl

Macros to define and implement interfaces, to ensure they are checked and correct.
MIT License
72 stars 4 forks source link

show an interface item that is a function #2

Closed JeffreySarnoff closed 4 months ago

JeffreySarnoff commented 1 year ago
module Animals

using Interfaces

# Define the methods the interface uses
function age end
function walk end
function talk end
function dig end

# Define the interface conditions
@interface AnimalInterface (
    mandatory = (;
        age = (
            x -> age(x) isa Real,
            x -> age(x) >= 0,
        )
    ),
    optional = (;
        walk = x -> walk(x) isa String,
        talk = x -> talk(x) isa Symbol,
        eat = x -> eat(x) isa Function,            # <<<<<<< show what to do with a function
        eat = (x,y) -> eat(x,y) isa Function,    # <<<<<<< is multidispatch available?
    ),
)

end
using Interfaces

# Define our Duck object
struct Duck
    age::Int
end

# And extend Animals methods for it
Animals.age(duck::Duck) = duck.age
Animals.walk(::Duck) = "waddle"
Animals.eat(::Duck) = (x,y::Food)->isduckfood(y) ? true : false # <<<< making it up

# And define the interface
@implements Animals.AnimalInterface{(:walk, :talk)} Duck Duck(2)
rafaqz commented 1 year ago

Sure. PR?

Functions are only of one object - the test object in question. If there are multiple test objects the are just mapped over individually.

So this isn't a thing:

eat = (x,y) -> eat(x,y) isa Function

Additionally, the eat condition can only be used once, because its a key in a NamedTuple - instead you just return a tuple of functions if it contains multiple subconditions.

But I'm sure you can hack it by passing is a Tuple as the test object.

gdalle commented 9 months ago

So this isn't a thing

It will be with #16

rafaqz commented 9 months ago

Still isnt really a thing but Arguments is an alternative

(Although we could easily transform it to use Arguments with the macro now)

rafaqz commented 4 months ago

This is taken care of with Arguments