All Projects → athos → type-infer

athos / type-infer

Licence: other
A Clojure utility to inspect static types inferred by the Clojure compiler

Programming Languages

clojure
4091 projects

Projects that are alternatives of or similar to type-infer

Zod
TypeScript-first schema validation with static type inference
Stars: ✭ 5,371 (+33468.75%)
Mutual labels:  static-types, type-inference
DataAnalyzer.app
✨🚀 DataAnalyzer.app - Convert JSON/CSV to Typed Data Interfaces - Automatically!
Stars: ✭ 23 (+43.75%)
Mutual labels:  type-inference
REInfer
Runtime Extended Inference for json data.
Stars: ✭ 23 (+43.75%)
Mutual labels:  type-inference
visions
Type System for Data Analysis in Python
Stars: ✭ 136 (+750%)
Mutual labels:  type-inference
HyperKA
Knowledge Association with Hyperbolic Knowledge Graph Embeddings, EMNLP 2020
Stars: ✭ 27 (+68.75%)
Mutual labels:  type-inference
Typology
Swift type checking and semantic analysis for developer tools
Stars: ✭ 68 (+325%)
Mutual labels:  type-inference
samlang
Sam's Programming Language
Stars: ✭ 22 (+37.5%)
Mutual labels:  type-inference
heron-language
A pure functional type-inferred language based on JavaScript
Stars: ✭ 53 (+231.25%)
Mutual labels:  type-inference
TypeInferencer
Algorithm W and Algorithm M in F#
Stars: ✭ 33 (+106.25%)
Mutual labels:  type-inference
TypesAndProofs
Type inference algorithms and intuitionistic propositional theorem provers solving type inhabitation problems
Stars: ✭ 23 (+43.75%)
Mutual labels:  type-inference
LambdaNet
Probabilistic Type Inference using Graph Neural Networks
Stars: ✭ 39 (+143.75%)
Mutual labels:  type-inference
vanilla-lang
An implementation of a predicative polymorphic language with bidirectional type inference and algebraic data types
Stars: ✭ 73 (+356.25%)
Mutual labels:  type-inference
phpstan-dba
PHPStan based SQL static analysis and type inference for the database access layer
Stars: ✭ 163 (+918.75%)
Mutual labels:  type-inference
redux-typed-saga
A type-safe alternative to redux-saga. // Using `yield*` in `finally` is currently broken in Babel / ES spec, as it will terminate the generator completely.
Stars: ✭ 12 (-25%)
Mutual labels:  static-types
Gluon
A static, type inferred and embeddable language written in Rust.
Stars: ✭ 2,457 (+15256.25%)
Mutual labels:  type-inference
Jedi
Awesome autocompletion, static analysis and refactoring library for python
Stars: ✭ 5,037 (+31381.25%)
Mutual labels:  type-inference
Psalm
A static analysis tool for finding errors in PHP applications
Stars: ✭ 4,523 (+28168.75%)
Mutual labels:  type-inference
Write You A Haskell
Building a modern functional compiler from first principles. (http://dev.stephendiehl.com/fun/)
Stars: ✭ 3,064 (+19050%)
Mutual labels:  type-inference

type-infer

Clojars Project build

A Clojure utility to inspect static types inferred by the Clojure compiler

Installation

Add the following to your project :dependencies:

Clojars Project

Usage

infer

The infer macro tells us the static type of the given expression inferred by the Clojure compiler:

(require '[type-infer.core :refer [infer]])

(infer 42) ;=> long
(infer false) ;=> java.lang.Boolean
(infer "foo") ;=> java.lang.String

It can take an arbitrary expression and returns its static type as long as the Clojure compiler can infer:

(infer (not false)) ;=> java.lang.Boolean
(infer (/ 1 2)) ;=> java.lang.Number
(infer (if (even? 2) :even :odd)) ;=> clojure.lang.Keyword
(infer (let [x 2 y (inc x)] (* x y))) ;=> long
(infer (fn [x] x)) ;=> clojure.lang.AFunction

Otherwise (i.e. the compiler failed to infer the static type of the given expression), infer returns nil:

(infer (identity 42)) ;=> nil
(infer (if (even? 2) "foo" false)) ;=> nil
(infer cons) ;=> nil

The infer macro is mainly intended to be used for performance tuning, and is especially useful for removing reflection warnings and taking full advantage of primitive types.

In most cases, infer per se is difficult to use in your function or macro. If you want to use it in your macro, it's highly recommend to use infer-type below instead.

infer-type

The infer-type fn takes two arguments: The first one is the implicit macro argument &env and the second one is a symbol. To get &env, you need to call infer-type in a macro:

(require '[type-infer.core :as ty])

(defmacro my-infer-type* [sym]
  (ty/infer-type &env sym))
 
(def ^String s "foo")
(my-infer-type s) ;=> java.lang.String

(let [x 42]
  (my-infer-type x)) ;=> long

Note that infer-type only accepts a symbol as its second argument. If you want to pass arbitrary expression to it, you'll need to pass the expression via an extra let form like the following:

(defmacro my-infer-type [x]
  `(let [x# ~x]
     (my-infer-type* x#)))

(my-infer-type (fn [x] x)) ;=> clojure.lang.AFunction

Note also that due to the above limitation, the target expression of the type inference may be evaluated at most once. This means that it is impossible (or difficult at best) to implement macros using infer-type such that the input expression is never evaluated in the expanded form.

Practical use

Examples of practical use of the library can be found in the following projects:

License

Copyright © 2021 Shogo Ohta

This program and the accompanying materials are made available under the terms of the Eclipse Public License 2.0 which is available at http://www.eclipse.org/legal/epl-2.0.

This Source Code may also be made available under the following Secondary Licenses when the conditions for such availability set forth in the Eclipse Public License, v. 2.0 are satisfied: GNU General Public License as published by the Free Software Foundation, either version 2 of the License, or (at your option) any later version, with the GNU Classpath Exception which is available at https://www.gnu.org/software/classpath/license.html.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].