LispSyntax.jl icon indicating copy to clipboard operation
LispSyntax.jl copied to clipboard

Decide on syntax for optional type declaration

Open MasonProtter opened this issue 5 years ago • 3 comments

We'll need a type declaration syntax at least for method definitions.

I was thinking something like this

f(x::Number, y::String) = (x, y)  

translating to

(defn f [(x :: Number) (y :: String)]) (tuple x y))

which leads to the question of parametric types and where. I'm not as sure about how to deal with those.

MasonProtter avatar Jun 11 '19 22:06 MasonProtter

I’ve run into this in other Lisps, too ... we explored it in LFE a while ago; more recently in a Go Lisp I’ve been playing with, I explored all sorts of possibilities:

  • https://github.com/zylisp/zylisp/issues/49

Ultimately, I think I’d go with the following (mostly to stay closer to Go):

(type point struct
  [X float64]
  [Y float64])

(defn distance
  [a *Point b *Point] float64
  (math/Sqrt
    (+ (math/Pow (- b.X a.X) 2)
       (math/Pow (- b.Y a.Y) 2))))

This seems to be analogous to what you have proposed, namely staying close to the parent language:

(struct Point
  x ::Float64
  y ::Float64)

(defn distance
  [(a ::Point) (b ::Point)] ::Float64
  (sqrt
    (+ (^ (- b.x a.x) 2)
       (^ (- b.y a.y) 2))))

Although, I might rather have the Lisp do the interleaving of the args/types for me?

(defn distance
  [a ::Point b ::Point] ::Float64
  (sqrt
    (+ (^ (- b.x a.x) 2)
       (^ (- b.y a.y) 2))))

oubiwann avatar Jun 12 '19 01:06 oubiwann

worth a discussion. there are lots of ways to do this including the racket and clojure (core.typing) ways. As julia has parametric types with 'where' conditions, that would have to be thought through too.

swadey avatar Jun 12 '19 15:06 swadey

Since LispSyntax.jl is clojure-flavored, maybe syntax borrowed from defmethod would be a good analogy? https://clojuredocs.org/clojure.core/defmulti

Or alternatively, perhaps it would be better to move away from the focus on clojure per se and focus on a lispy syntax which is as close as julia as possible. I think this is probably my preferred option if the mission of LispSyntax.jl is to be a lispy veneer over julia semantics.

c42f avatar Jun 15 '19 05:06 c42f