logoalt Hacker News

LightMachineyesterday at 11:09 PM1 replyview on HN

Ohhh. Ok. I see the confusion. That's bad syntax then. The 'a' parameter is not the type of the elements. 'A' is the type of the elements. 'a' is just the "quantity of the type of the type of the elements". Yes, that's a mouthful, and somewhat abstract. This is similar to Agda's universe polymorphism. The reason it exists is because there are two "types of types" (i.e., kinds). Copiable types, and non-copiable types. So, this argument allows you to parametrize a function on both of these kinds. That way, you don't need to write List.length twice: one for copiable types, and once for non-copiable types. Think of Rust generics. 'a' is playing the same role as a "Copiable" trait would in Rust. It is just an extra compile-time argument to avoid boilerplate. But yes, I see how this can be confusing and I think I know how to improve that syntax.

Yes, `Array<U32> & U32` is just `(Array<U32>, U32)` and now that you point it I believe I made a bad choice, no excuses. Also, `arr[3]` doesn't return a number. It returns a copy of the same array, plus a number. So, if the element at index 3 is 123, tthen, `arr[3]` will return `(arr, 123)`. Now, you might be thinking: that's terrible. And yes, it is. I realize it now. I should have made the `arr[3]` syntax return 123. It is there for a very good reason though. It preserves linearity. It is part of the termination argument that makes Bend consistent. But yes, exposing it to the end user was most likely a mistake. I will redesign that syntax. Sorry about it.


Replies

amlutotoday at 2:32 AM

The array thing is messy. It seems to me that there are potentially four kinds of arrays: the array itself can be copyable or not, and the contents can be copyable or not. But an array of copyable objects can be copied (possibly inefficiently depending what you're doing) whether the creator of the array wants you to or not, and a copyable array of noncopyable objects lets you copy the objects by copying the array. So maybe only two cases are really useful: when the copyability of the array matches the copyability of the objects.

In the everything-copyable case, you can just read an element.

In the nothing-copyable case, the syntax is irrelevant: the operation (arr, elem) = arr.read(index) is invalid.

You may want to take a look at how Rust deals with this. In Rust, even if T: !Copy, you can take a reference to an array element. If a language can't manage this sort of reference, you may need a more restrictive mechanism, perhaps as a pair of swaps (but then you need a default value) or some mechanism using closures that get called on the element and are required to return it.