cognitect-labs / transducers-js

Transducers for JavaScript
Apache License 2.0
1.6k stars 44 forks source link

Add .type field to transducers #37

Open tomaskulich opened 7 years ago

tomaskulich commented 7 years ago

When creating a library which accepts transducers, it is useful to warn the users, if they mixed up the arguments. For example in

function createChannel(transducer = null) {}

it would be great if createChannel can (runtime) assert that it got the transducer and not e.g. string. For this purpose, .type attribute attached to transducer fn with value such as 'transducers-js' could be used. Another possibility is to add .@@__is_transducer__@@ attribute set to true (idea copied from https://github.com/facebook/immutable-js/blob/master/src/Iterable.js#L38)

Currently transducers are functions. This means we can distinguish them from strings, numbers and objects, but still, there is a lot of other functions that can be easily mixed up with transducers.

Finally, if someone wants to implement transducer protocol by themselves, they can assign the .type manually. This is however rather rare scenario.