Skip to content
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

show an interface item that is a function #2

Closed
JeffreySarnoff opened this issue Jul 10, 2022 · 4 comments
Closed

show an interface item that is a function #2

JeffreySarnoff opened this issue Jul 10, 2022 · 4 comments

Comments

@JeffreySarnoff
Copy link

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
Copy link
Owner

rafaqz commented Jul 10, 2022

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
Copy link
Collaborator

gdalle commented Oct 2, 2023

So this isn't a thing

It will be with #16

@rafaqz
Copy link
Owner

rafaqz commented Oct 2, 2023

Still isnt really a thing but Arguments is an alternative

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

@rafaqz
Copy link
Owner

rafaqz commented Mar 1, 2024

This is taken care of with Arguments

@rafaqz rafaqz closed this as completed Mar 1, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants