most languages come up with a way to distinguish readonly and mutable variables, and oh-lang is no different. in Rust,
why? well, like python, oh-lang doesn't need
what do you get with this novelty? well, it does allow defining methods that can be either readonly or mutating methods, as a "constant template". in Rust, you often need to implement something like
as one final note, in general oh-lang will allow an expression like
let x = 3; is a readonly, while let mut y = 4; is mutable. the same thing in C might be const int x = 3; and int y = 4;. oh-lang goes all in on concision and writes these as x: 3 and y; 4. yes, oh-lang makes the questionable life choice of appropriating everyone's favorite statement ender™ ; for mutable variable declarations.
why? well, like python, oh-lang doesn't need
; to end statements. we infer you are trying to end a statement by various implicit rules (like doing a newline without tabbing), or explicitly using ,. that frees ; for something else, and because it looks like :, which we use for declaring readonly variables, we decided it would make sense to declare writable variables. we also use it for "short-hand" to declare methods that can mutate the class instance (;;my_mutating_method()), in analogy with methods that keep the class instance readonly (::my_readonly_method(), familiar to C++/Rust/Java devs).
what do you get with this novelty? well, it does allow defining methods that can be either readonly or mutating methods, as a "constant template". in Rust, you often need to implement something like
Index and the corresponding IndexMut because mut is a new keyword rather than a different type of declaration. consider the following wrapper class in Rust:
// Rust
struct MyStruct {
my_array: Vec<i32>,
}
impl std::ops::Index<usize> for MyStruct {
type Output = i32;
fn index(&self, index: usize) -> &Self::Output {
&self.my_array[index]
}
}
impl std::ops::IndexMut<usize> for MyStruct {
fn index_mut(&self, index: usize) -> &mut Self::Output {
&mut self.my_array[index]
}
}
in oh-lang, the equivalent would look like this, noting that #(x: #type) is a reference type (to a readonly value named x of type #type), so #(int32:)/#(int32;) is a readonly/mutable reference to a 32-bit signed integer:
#my_struct:
#[ my_array: #array[#i32]
]
{ ::[usize.]: #(i32:)
m my_array[usize]
;;[usize.]: #(i32;)
m my_array[usize]
}
which is already an verbosity improvement (sorry Rust, i don't like traits), but we can do better with constant templates:
#my_struct:
#[ my_array: #array[#i32]
]
{ :;[usize.]: #(i32:;)
m my_array[usize]
}
using :; (or ;:) will create two different methods, and will choose the respective readonly/mutable reference return type as expected!
as one final note, in general oh-lang will allow an expression like
some_var to implicitly resolve into a reference type #(some_var: #whatever_type) or even #(whatever_type:) so we don't need to do things like &some_var in Rust. this was for consistency with UFCS method calls: my_instance my_method(3) should work the same as my_method(my_instance, 3). by default, whatever constant-type the variable is will be what method type it tries to call, since methods can be overloaded for readonly/mutable instances as above, but you can request a readonly method on a writable method explicitly using my_instance::my_method(3) or my_method(:my_instance, 3).
Comments
Post a Comment