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

idea: interface validation #75

Closed
kobi2187 opened this issue Nov 4, 2021 · 5 comments
Closed

idea: interface validation #75

kobi2187 opened this issue Nov 4, 2021 · 5 comments

Comments

@kobi2187
Copy link

kobi2187 commented Nov 4, 2021

Many people use interface as a design tool.
perhaps it is possible to use Nim's when compiles(...) feature
to expand something like checkImpl(myClass, ISomeInterface)
where ISomeInterface will be scanned (typetraits perhaps) for the public procs and fields, and then for each it will be checked in the (hidden by template) when compiles block. then we get a compiler error message, that this proc was not yet implemented.
What do you think of this idea? btw, very cool lib. just came across it!

@glassesneo
Copy link
Owner

glassesneo commented Nov 6, 2021

That's nice idea. I've been thinking about interface for a while and I was going to implement it like this:

# `interface` is a reserved word so `protocol` is used instead.
protocol IFly:
  proc fly() =
    discard

class Bird impl IFly:
  proc fly() =
    echo "I'm flying!"

and then it's converted below like nim-interface-implements:

type IFly = tuple
  fly: proc()

type Bird = ref object

proc fly(self: Bird) =
  echo "I'm flying!"

proc toProtocol(self: Bird): IFly =
  return (
    fly: self.fly()
  )

I'm interested in your idea, but I can't think of a way to implement it. Can you tell me some details?

@kobi2187
Copy link
Author

kobi2187 commented Nov 7, 2021

I see. very nice. the syntax looks very clean and clear.
There is (yet) another interface implementation called iface from yglukhov.

The details need to be fleshed out. I am not sure it works.
Nim has a compiler feature to check if code compiled (checked during compile time)
https://nim-lang.github.io/Nim/system#compiles%2Cuntyped

when compiles(3 + 4):
  echo "'+' for integers is available"

so if we do:

# check_bird_fly
when not compiles(new Bird().fly()):
  {.error "fly proc not yet implemented for Bird type".}

perhaps it's a simple solution. maybe make it more explicit like:
checkImpl(Bird,IFly), then it will expand to try all the IFly defined procs for Bird.

is that clearer now? None of the code was checked, it was just an idea. see if u can get it to work... Thanks!

@glassesneo
Copy link
Owner

I see, I understand.
So the type of the first argument of checkImpl() is typed, right?
I think I can implement it well, so I'll try. Thanks!

@glassesneo glassesneo mentioned this issue Nov 7, 2021
4 tasks
@glassesneo
Copy link
Owner

I just created an issue based on this page.

@kobi2187
Copy link
Author

kobi2187 commented Nov 7, 2021

cool :-)

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

2 participants