adobe / ferrum

Features from the rust language in javascript: Provides Traits/Type classes & a hashing infrastructure and an advanced library for working with sequences/iterators in js
https://www.ferrumjs.org
Apache License 2.0
519 stars 25 forks source link

runtime type signatures (type casting/constriction) #128

Open koraa opened 4 years ago

koraa commented 4 years ago

Can we introduce type signatures using plain javascript?

Used to take all arguments to their respective types. (Use with casting functions like obj, etc…)

typedfn = (types, fn) => (...args) => fn(...map(zip(types, args), (t, v) => t(v))));
typecasg = (fn, types) => 

But typefn should correctly indicate function arity.

e.g.

addNumbers = typedfn([Number, Number], plus);
concatStrings = typedfn([String, String], plus);

This constricts arbitrary argument types by casting them into the correct type.