Coder Social home page Coder Social logo

Automatic argument checking about runtypes HOT 2 CLOSED

yuhr avatar yuhr commented on May 18, 2024
Automatic argument checking

from runtypes.

Comments (2)

pelotom avatar pelotom commented on May 18, 2024

This sounds like the purpose of contracts, although I haven’t tried using them as a decorator. If there’s something that we could do to make them more idiomatic for that purpose I’d be open to it.

from runtypes.

yuhr avatar yuhr commented on May 18, 2024

Yes of course, so contracts look like:

method = Contract(Runtype1, Runtype2, Runtype3)
  .enforce((param1: Static1, param2: Static2, param3: Static3) => {
    ...
  })

However, my understanding is if one wants to write a single function then a contract should fit (and calling an external function may also fits), but for implementing class methods, usually there's no need to type-check the returning value as it is already in the internal type-safe world, that is, returning a member, a return value from another method, or something else which is try-caught or guarded within their mechanism.

And another advantage is that we can skip trailing params:

@checked(Runtype1, Runtype2)
method(param1: Static1, param2: Static2, param3: any) {
  ...
}

Also we can utilise parameter decorators for marking which params shall be type-checked:

@checked(Runtype1, Runtype3)
method(@check param1: Static1, param2: any, @check param3: Static3) {
  ...
}

or kind of

@checked
method(@check(Runtype1) param1: Static1, param2: any, @check(Runtype3) param3: Static3) {
  ...
}

We use Always in the case of contracts, but it's somewhat verbose I think.

from runtypes.

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.