logoalt Hacker News

chonglilast Sunday at 11:58 PM1 replyview on HN

Can you replace 10 5 and 3 3 with variable names and still get the warning without first calling the function?


Replies

tmtvllast Monday at 12:24 PM

No. You can replace them with asterisks to mean 'any length is fine', but you can't at compile time check something that's only known at runtime (granted, you could see that it can't match if the variables are, for example, an (Integer 1 10) and an (Integer 75 100)). Something you can do with variables is:

  (defvar *example-dimension* 20)

  (deftype Example-Array (element-type minor-dimension)
    `(Array ,element-type (,*example-dimension* ,minor-dimension)))
...and then you can declare variables to be something like, say, (Example-Array Boolean 5) and have it expand to (Array Boolean (20 5)). But the type declaration needs to be of the form (Array &optional element-type dimensions) where dimensions can be any of the following:

- An asterisk, which means anything goes.

- A number, which means the array needs to have that many dimensions (so an (Array Fixnum 2) is a 2-dimensional array of fixnums).

- A list of numbers, asterisks, or a combination of the two, where an asterisk means 'this dimension can be anything' and the numbers mean exactly what you would expect.

Maybe something like that would be possible with Coalton, but I haven't played around a lot with that yet.

show 1 reply