admin-announcements

Announcements from the Clojurians Admin Team (@U11BV7MTK @U077BEWNQ @U050TNB9F @U0ETXRFEW @U04V70XH6 @U8MJBRSR5 and others)
sveri 2015-12-19T08:05:12.002132Z

@akiva: @jaen @gfredericks Thanks for the suggestions, as I just have a very simple usecase now I go with data.json and provide a value function.

sveri 2015-12-19T08:15:07.002133Z

Hm, does prismatic have a concept of generics? What I want is to declare that, whatever comes into that function, has to be returned too. Is that possible?

jaen 2015-12-19T08:36:59.002134Z

Not sure what that means exactly, any example use case?

sveri 2015-12-19T09:07:52.002136Z

@jaen: If I pass in a date, then I want to return a date, if I pass in a String, I want to return a string, if I pass in T I want to return T.

jaen 2015-12-19T09:13:26.002137Z

By "prismatic" you mean schema, yes? So doesn't (schema.core/validate s/Int 12) already do it?

jaen 2015-12-19T09:13:31.002138Z

Or am I misunderstanding something?

sveri 2015-12-19T09:29:37.002140Z

@jaen: Yes, I mean prismatic schema. These are two functions, both receive a parameter of whatever type

sveri 2015-12-19T09:29:42.002141Z

and may return the same type

sveri 2015-12-19T09:30:05.002142Z

This could be an invariant of a function, whatever type comes in, has to be returned

sveri 2015-12-19T09:31:13.002143Z

Does that make more sense?

jaen 2015-12-19T09:33:32.002144Z

How about

(s/set-fn-validation!)

(s/defn function :- ArgSchema [arg :- ArgSchema] ...)
this will verify your function takes and return the same shape of data (or throw otherwise). Is that something you mean?

sveri 2015-12-19T09:39:30.002145Z

No, I dont talk about the shape directly, although, in a prismatic sense it might be the same. I am not sure how else to express this. Maybe you could have a look at: https://docs.oracle.com/javase/tutorial/java/generics/types.html "A Generic Version of the Box Class" if you want. The closest thing I can imagine is:

sveri 2015-12-19T09:40:26.002148Z

But this would also accepting that param is a Str, but returns an Inst

sveri 2015-12-19T09:40:31.002149Z

Which is not what I want

jaen 2015-12-19T09:54:02.002151Z

Ahh, so you mean like A -> A.

jaen 2015-12-19T09:59:28.002152Z

For some reason I didn't catch on you wanted parametrically polymorphic function with same parameter and return type, sorry. Was to caught in the schema part of the question.

jaen 2015-12-19T09:59:31.002153Z

I'm not sure if schema supports any way of relating the parameter schemas to the return schema. Sounds like a core.typed thing.

jaen 2015-12-19T10:10:00.002155Z

I imagine you could cheat with a conditional schema, like so:

(defmulti make-return-schema-for-fun (fn [& args] (mapv class args)))
(defmethod make-return-schema-for-fun [String] [_] s/Str)
(defmethod make-return-schema-for-fun [Integer] [_] s/Int)
(defmethod make-return-schema-for-fun [Long] [_] s/Int)

(defn fun [arg1]
  (let [return-value (* 2 (Integer. arg1))] ; calculate function return here
    (s/validate (make-return-schema-for-fun arg1) return-value)
    return-value))
but that doesn't look too nice to me TBH...

jaen 2015-12-19T10:10:21.002156Z

This code above will work with (fun 2) but fail for (fun "2") with a schema error, because input type doesn't match output type.

jaen 2015-12-19T10:10:30.002157Z

But again, that feels... weird, to say the least.

sveri 2015-12-19T10:12:38.002158Z

Interesting solution, but its more handcrafted and I agree, the logic itself is hidden inside 10 lines boilerplate code

sveri 2015-12-19T10:13:21.002159Z

Well, it's nice to finally see the limits of prismatic schema

jaen 2015-12-19T10:16:53.002160Z

Well, I guess it might have looked a bit nicer using actual conditional schema or that newfangled abstract schema map, but would still feel like a hack to me nonetheless.

jaen 2015-12-19T10:20:58.002161Z

(def FunSchema
  (s/conditional
    (comp string? :arg1)
      {:arg1 s/Str :return s/Str}

    (comp integer? :arg1)
      {:arg1 s/Int :return s/Int}))

(defn fun [arg1]
  (let [return-value (* 2 (Integer. arg1))] ; calculate function return here
    (s/validate FunSchema {:arg1 arg1 :return return-value})
    return-value))

jaen 2015-12-19T10:21:09.002162Z

You could macro it up to avoid writing out the boilerplate each time

jaen 2015-12-19T10:21:16.002163Z

But I would still feel somewhat iffy using this.

sveri 2015-12-19T10:24:22.002166Z

@jaen: Thanks, I'll take a not of it if I need it once really hard I might use it. But for my current usecase this is to much overload