cljs-dev

ClojureScript compiler & std lib dev, https://clojurescript.org/community/dev
2019-11-06T13:08:35.176600Z

@mfikes Would it make sense to propagate function return type through apply call?

mfikes 2019-11-06T13:11:56.177800Z

Hrm, perhaps you can statically deduce that

2019-11-06T13:12:31.178700Z

unless target function is of multiple arities

mfikes 2019-11-06T13:13:10.179800Z

Along those lines there might be a whole class of “higher order” inference algorithms

2019-11-06T13:14:27.180500Z

could you give an example?

mfikes 2019-11-06T13:20:30.180900Z

identity

2019-11-06T13:21:28.181200Z

alright 🙂

2019-11-06T13:26:15.183600Z

@mfikes looks like return type of apply (`any`) is prioritized over return type hint, is this intentional?

2019-11-06T13:26:36.184600Z

(defn ^number f [x] x)
(apply f [1]) ;; tag here is `any`

mfikes 2019-11-06T13:26:40.184800Z

That’s a trivial example, but there are probably semi-complicated higher order cases where, as a human you can statically deduce types where the compiler is not yet doing so.

2019-11-06T13:26:56.185100Z

oh wait, the example is wrong

mfikes 2019-11-06T13:27:33.186Z

FWIW, on the surface, I think your apply idea is valid

2019-11-06T13:29:24.187400Z

hm, this is a different question, but I’m hitting a weird case where return type of fx is any even though there’s return type hint

(defn f [x] x)
(defn ^number fx []
    (apply f [1]))
(fx)

2019-11-06T13:29:33.187700Z

can’t repro in master though

2019-11-06T13:30:21.189Z

placing the hint onto return value itself seem to fix this

mfikes 2019-11-06T13:30:49.189700Z

Yeah, that would be odd. Manual hints should override inferred types

alexmiller 2019-11-06T13:32:16.192400Z

Isn’t that type hint in the wrong place?

mfikes 2019-11-06T13:32:17.192500Z

I think with apply you’d need to be careful in the multi-arity case to ensure that, you get the union of all return types

alexmiller 2019-11-06T13:32:52.193800Z

Should be on the arg vector

mfikes 2019-11-06T13:32:54.193900Z

Nah, Alex. ClojureScript differs on type hint placement

mfikes 2019-11-06T13:32:59.194100Z

¯\(ツ)

alexmiller 2019-11-06T13:33:01.194300Z

Ah, sorry

mfikes 2019-11-06T13:33:23.195Z

I think there is a JIRA to unify with Clojure

2019-11-06T13:40:20.195800Z

@mfikes Should I file a JIRA then for higher order inference?

mfikes 2019-11-06T13:44:51.196900Z

@roman01la Off the top of my head, I don’t know of any general algorithm. Maybe it makes sense to do specific things like apply, and see if any generaliztion falls out of those kinds of things?

2019-11-06T13:45:23.197200Z

yeah I think it should be a good start

2019-11-06T13:45:37.197600Z

identity should be trivial as well

mfikes 2019-11-06T13:46:00.198300Z

An example of a complex higher order inference might be (first (map inc [1])) being of numeric type.

mfikes 2019-11-06T13:46:41.198900Z

As a human, you can easily see this is the case, but the compiler is far away from being able to handle stuff like that.

mfikes 2019-11-06T13:49:19.200100Z

I suspect, that even if we managed to do higher order inference, the first baby steps would likely heavily rely on static knowledge of the semantics of core functions (as opposed to being able to do similar inference for user-defined functions)

dnolen 2019-11-06T14:00:29.200400Z

we should probably start putting release notes together

mfikes 2019-11-06T14:08:25.200900Z

Yeah, I'll start a PR for release notes 👍