Fork me on GitHub
#beginners
<
2016-08-25
>
donaldball00:08:22

I very seldom use proxy so I’m probably doing something wrong that’s elementary. I’m trying to proxy OutputStream and implement the write(byte b) method. Unfortunately, it’s not working:

donaldball00:08:37

(.write rwos (byte 1))
IllegalArgumentException No matching method found: write for class bleach.outputstream.proxy$java.io.OutputStream$ff19274a  clojure.lang.Reflector.invokeMatchingMethod (Reflector.java:80)

donaldball00:08:39

It seems as if the write fn is only accepting the byte[] argument. If I add a ^byte type hint, the compiler yells at me:

CompilerException java.lang.IllegalArgumentException: Only long and double primitives are supported

seancorfield02:08:41

@bj At that sort of level I’d probably look at :pre / :post assertions on the function itself — although I would only bother with such validation at subsystem boundaries rather than on every function. If you want that sort of protection, you probably want a statically typed language instead of Clojure...

seancorfield02:08:07

@bj And bear in mind that map can be applied to things that, for example, coll? will return false on so you’ll have to be pretty careful about which predicates you use for such validation.

seancorfield02:08:27

(the convenient one here I think is seqable? but that’s new in Clojure 1.9.0)

chris14:08:12

@bj you can use plumatic/schema, but anything more than simple validation is a bit beyond the scope of what clojure is trying to do. Doing validation at boundaries would be a good idea, but beyond that I wouldn’t. If you get the wrong type it’ll throw an exception anyways

bj15:08:54

Thanks for the help. I’ll stick to validation only at boundaries

benjyz18:08:18

is there a special operator for this?

(filter #(= (:key %) :selected-key) vector-of-maps)

oahner19:08:01

@benjyz not really, altho you can achieve the same thing with comp and a set, but that's overkill unless you want to match against multiple keys at once