Giter Site home page Giter Site logo

idea: interface validation about oolib HOT 5 CLOSED

kobi2187 avatar kobi2187 commented on June 26, 2024
idea: interface validation

from oolib.

Comments (5)

glassesneo avatar glassesneo commented on June 26, 2024 1

I just created an issue based on this page.

from oolib.

kobi2187 avatar kobi2187 commented on June 26, 2024 1

cool :-)

from oolib.

glassesneo avatar glassesneo commented on June 26, 2024

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?

from oolib.

kobi2187 avatar kobi2187 commented on June 26, 2024

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!

from oolib.

glassesneo avatar glassesneo commented on June 26, 2024

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!

from oolib.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.