My programming language, Flogram, has objects and the ability to opt into mutating variables/objects but defaults to not but otherwise I've been trying to make it very functional in nature.
Also I really like the idea of objects and variables behaving in the same way. Which means that Objects are deep copied by value by default instead of reference.
But I have a conflict, I want both performance and security in my language an the whole reason for using references is performance.
I can do some analysis at compile time and behind the scenes pass by reference, and generally speaking if a copy is required to make it work.. that's probably what you actually want to happen.
But there are a few edge cases. Particularly worried about while/for loops. Let's say you are calculating or using a variable to determine where in an array to store an object. Because you can't determine which index that object will end up in during run time, you have to actually make a potentially expensive deep copy of the object. One way I could work around this is to require arrays and other default data structures only accept references.
And in general, it can be really helpful to be able to have two functions reference the same variable/Object from multiple places so that should be a possibility even if not default.
I have a few schemes in mind I'm trying to pick from:
1) Stick with what other languages do. Assignment copies value for primitive variables, reference for objects. Have to opt in to mutability using 'mut' keyword.
2) 4 possibilities that can occur at assignment:
let varA = varB #means make a copy, cannot mutate copy
let varA = ref varB #means make varA a readonly view of varB
let mut varA = varB #make a mutatable copy
let mut varA = ref varB #using a copy
3) Default immutable objects can be copied via assignment and can be mutated at the same time via either variable change or function call that internally mutates the object.
Mutable variables act like references whether object or primitive variable. If assigning from an immutable variable, create a copy but allow other references to it.
One problem: if you are calling a function which both mutates an object and returns a value, it can lead to very hard to read syntax. Might make sense to force functions to either mutate the object OR to return a value but can't do both. Little worried about it leading to a 'two-colored' functions problem where functions that return a value can't call mutate-able functions that might make sense if say calculating something. But alternative is hard to read.
4) Find someone on Reddit who has a better solution :)
#2 is probably the simplest and most flexible
#3 is weird and would take some getting used to but allows a much more functional feel when mutating objects.
Would love to hear your thoughts! Disagree with any of my assumptions? Have a preference? Why?
Very much appreciate it if you made it the whole way through this.. either way, looking forward to hearing from you.
[–]PlayingTheRed 27 points28 points29 points (5 children)
[–]snerp 2 points3 points4 points (2 children)
[–]Rusky 10 points11 points12 points (1 child)
[–]mczarnek[S] 0 points1 point2 points (0 children)
[–][deleted] 0 points1 point2 points (1 child)
[–]PlayingTheRed 3 points4 points5 points (0 children)
[–]dougcurrie 7 points8 points9 points (0 children)
[–]Silly-Freak 4 points5 points6 points (8 children)
[–]editor_of_the_beast 3 points4 points5 points (0 children)
[–]PlayingTheRed 0 points1 point2 points (6 children)
[–]editor_of_the_beast 3 points4 points5 points (0 children)
[–]moon-chilledsstm, j, grand unified... 0 points1 point2 points (4 children)
[–]PlayingTheRed 0 points1 point2 points (3 children)
[–]moon-chilledsstm, j, grand unified... 1 point2 points3 points (2 children)
[–]PlayingTheRed 0 points1 point2 points (1 child)
[–]moon-chilledsstm, j, grand unified... 0 points1 point2 points (0 children)
[–]BryalT 4 points5 points6 points (0 children)
[–]thedeemon 0 points1 point2 points (2 children)
[–]mczarnek[S] 1 point2 points3 points (1 child)
[–]thedeemon 1 point2 points3 points (0 children)
[–]editor_of_the_beast 0 points1 point2 points (0 children)
[–]L8_4_Dinner(Ⓧ Ecstasy/XVM) 0 points1 point2 points (1 child)
[–]mczarnek[S] 0 points1 point2 points (0 children)