Carp/examples/selection.carp

56 lines
952 B
Plaintext
Raw Normal View History

2017-06-26 12:15:03 +03:00
;; Type selection (choose the right function based on the types at the call site)
2017-09-06 11:05:19 +03:00
(use IO)
(use Int)
2017-06-26 12:15:03 +03:00
;; USER DEFINED FUNCTIONS
(defn f [] true)
(defmodule A (defn f [] 123))
(defmodule B (defn f [] @"hello"))
2017-06-26 12:15:03 +03:00
2017-09-06 11:05:19 +03:00
(use A)
(use B)
2017-06-26 12:15:03 +03:00
(defn main []
(do
(if (f) (println "yes") (println "no"))
(println (ref (Int.str (f))))
2017-06-26 12:15:03 +03:00
(println (ref (f)))))
(build)
(run)
;; => yes
;; 123
;; hello
;; PLUS FUNCTION
2017-09-06 11:05:19 +03:00
(use Double)
(use Float)
2017-06-26 12:15:03 +03:00
(defn shouldBeInt [x] (+ x 10))
(defn shouldBeDouble [x] (+ x 10.0))
(defn shouldBeFloat [x] (+ x 10.0f))
(defn shouldBeFloatInLet [x] (let [a (+ x 10.0f)]
a))
;; RECORD FIELDS
(deftype Person [name String])
(deftype Building [name String])
2017-09-06 11:05:19 +03:00
(use Person)
(use Building)
2017-06-26 12:15:03 +03:00
(defn main []
(let [thing (Building.init @"Eiffel Tower")
another (Person.init @"Alice")]
2017-06-26 12:15:03 +03:00
(do (println (name (ref thing)))
(println (name (ref another))))))
(build)
(run)
2017-09-08 13:24:57 +03:00
(quit)