Skip to content

Commit 5ce4758

Browse files
authored
Primitive type coercion (#451)
* Primitive type coercion * Use repr in Python exc
1 parent 898ae20 commit 5ce4758

File tree

1 file changed

+96
-0
lines changed

1 file changed

+96
-0
lines changed

src/basilisp/core.lpy

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -983,6 +983,12 @@
983983
[x]
984984
(instance? python/bytes x))
985985

986+
(defn char?
987+
"Return true if x is a string of length 1."
988+
[x]
989+
(and (instance? python/str x)
990+
(= (python/len x) 1)))
991+
986992
(defn class?
987993
"Return true if x names a type."
988994
[x]
@@ -1234,6 +1240,96 @@
12341240
[x]
12351241
(= 0 x))
12361242

1243+
;;;;;;;;;;;;;;;;;;;
1244+
;; Type Coercion ;;
1245+
;;;;;;;;;;;;;;;;;;;
1246+
1247+
(defn bigdec
1248+
"Coerce x to a Decimal."
1249+
[x]
1250+
(decimal/Decimal x))
1251+
1252+
(defn bigint
1253+
"Coerce x to an integer.
1254+
1255+
Python's builtin `int` type is arbitrary precision, so there is no difference
1256+
between `bigint`, `biginteger`, and Python's builtin `int`."
1257+
[x]
1258+
(python/int x))
1259+
1260+
(defn biginteger
1261+
"Coerce x to an integer.
1262+
1263+
Python's builtin `int` type is arbitrary precision, so there is no difference
1264+
between `bigint`, `biginteger`, and Python's builtin `int`."
1265+
[x]
1266+
(python/int x))
1267+
1268+
(defn boolean
1269+
"Coerce x to a boolean."
1270+
[x]
1271+
(python/bool x))
1272+
1273+
(defn byte
1274+
"Coerce x to a byte."
1275+
[x]
1276+
(let [b (python/bytes [x])]
1277+
(if (= 1 (python/len b))
1278+
b
1279+
(throw
1280+
(python/ValueError (str "cannot coerce " (python/repr x) " to byte"))))))
1281+
1282+
(defn boolean
1283+
"Coerce x to a boolean."
1284+
[x]
1285+
(python/bool x))
1286+
1287+
(defn char
1288+
"Coerce x to a string of length 1.
1289+
1290+
Natural integers are treated as ordinals and passed to Python's `chr`.
1291+
Strings of length 1 are returned as such. Other types will result in
1292+
a `ValueError`."
1293+
[x]
1294+
(cond
1295+
(instance? python/int x) (python/chr x)
1296+
(char? x) x
1297+
:else (throw
1298+
(python/ValueError
1299+
(str "cannot cast " (python/repr x) " to char")))))
1300+
1301+
(defn double
1302+
"Coerce x to a float.
1303+
1304+
Python does not differentiate between `float` and `double`. Python `float`s are
1305+
double precision."
1306+
[x]
1307+
(python/float x))
1308+
1309+
(defn float
1310+
"Coerce x to a float."
1311+
[x]
1312+
(python/float x))
1313+
1314+
(defn int
1315+
"Coerce x to an integer."
1316+
[x]
1317+
(python/int x))
1318+
1319+
(defn long
1320+
"Coerce x to an integer.
1321+
1322+
Python does not support `long` types, so the value is coerced to an integer."
1323+
[x]
1324+
(python/int x))
1325+
1326+
(defn short
1327+
"Coerce x to an integer.
1328+
1329+
Python does not support `short` types, so the value is coerced to an integer."
1330+
[x]
1331+
(python/int x))
1332+
12371333
;;;;;;;;;;;;;;;;
12381334
;; Exceptions ;;
12391335
;;;;;;;;;;;;;;;;

0 commit comments

Comments
 (0)