From bff8ddd41691b020b36d5f567262b539b6de849b Mon Sep 17 00:00:00 2001 From: Bigjango13 Date: Mon, 30 Oct 2023 23:21:28 -0700 Subject: [PATCH 1/3] Initial draft of refs --- readme.md | 88 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 88 insertions(+) diff --git a/readme.md b/readme.md index 0467242..0d629bf 100644 --- a/readme.md +++ b/readme.md @@ -492,6 +492,94 @@ functi add_numbers ( a, b ) { } ``` +#### Reference arguments + +A special type of argument functions can take is called a reference argument. It's a refrence to variable or another argument. They are declared by putting the `ref` keyword before the argument name, and can be used by passing a reference (done by putting the `ref` keyword before a variable oe argument), for instance: +``` +functi append(ref list, item) { + # Appends item to list + list += [item]; +} + +let my_list = []; +append(ref my_list, 2); +# Prints "[2]" +print(my_list); +``` + + +When the first argument is a ref, you can use the dot operator on a variable to pass it by ref (the rest of the call continues as normal), so this example does the same thing as the previous one: +``` +functi append(ref list, item) { + # Appends item to list + list += [item]; +} + +let my_list = []; +# Exactly the same as `append(ref my_list, 2)` +my_list.append(2); +# Prints "[2]" +print(my_list); +``` + +All modifications to the reference argument are reflected onto the variable being referenced, and reads are done on the underlying value. For example: +``` +# This function does not work +functi set_to_zero(ref i) { + # This copies the underlying value, not the reference + let thing = i; + thing = 0; + # The correct way to do it is: + # i = 0; +} + +let not_zero = 47; +set_to_zero(not_zero); +# Because the value was copied it's still not zero, so this prints "47" +print(not_zero); +``` + + +Because of this, the `ref` keyword is still needed when passing a reference argument as a reference argument: +``` +functi add_one(ref i) { + i += 1; +} + +functi add(ref a, ref b) { + if (b == 1) { + # This ref keyword is required + add_one(ref a); + } else { + a += b; + } +} +``` + +Because it is illegal to pass a ref to a non-ref, or vice versa, Sack allows for them to be overloaded: +``` +functi add(a, b) { + return a + b; +} + +functi add(ref a, b) { + a += b; +} + +let foo = 4, bar = 7; + +# Calls the first add +add(1, 2); +add(foo, bar); +# Calls the second add +add(ref foo, bar); +foo.add(bar); +# Raises an error +add(foo, ref bar); +``` + + + ### Logical operators The following are valid logical operators in sack: From c3b6e6278d4625af091ad62eb153717033e05da6 Mon Sep 17 00:00:00 2001 From: Bigjango13 Date: Wed, 1 Nov 2023 21:31:35 -0700 Subject: [PATCH 2/3] Resolve ref issues --- readme.md | 4 ---- 1 file changed, 4 deletions(-) diff --git a/readme.md b/readme.md index 0d629bf..f26085d 100644 --- a/readme.md +++ b/readme.md @@ -507,7 +507,6 @@ append(ref my_list, 2); print(my_list); ``` - When the first argument is a ref, you can use the dot operator on a variable to pass it by ref (the rest of the call continues as normal), so this example does the same thing as the previous one: ``` functi append(ref list, item) { @@ -539,7 +538,6 @@ set_to_zero(not_zero); print(not_zero); ``` - Because of this, the `ref` keyword is still needed when passing a reference argument as a reference argument: ``` functi add_one(ref i) { @@ -578,8 +576,6 @@ foo.add(bar); add(foo, ref bar); ``` - - ### Logical operators The following are valid logical operators in sack: From 6fc1af57db887cc835a50795068ff3434a79e33c Mon Sep 17 00:00:00 2001 From: Nobody5050 Date: Thu, 8 May 2025 01:05:34 -0500 Subject: [PATCH 3/3] typo fixes --- readme.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/readme.md b/readme.md index f26085d..4afe612 100644 --- a/readme.md +++ b/readme.md @@ -494,7 +494,7 @@ functi add_numbers ( a, b ) { #### Reference arguments -A special type of argument functions can take is called a reference argument. It's a refrence to variable or another argument. They are declared by putting the `ref` keyword before the argument name, and can be used by passing a reference (done by putting the `ref` keyword before a variable oe argument), for instance: +A special type of argument functions can take is called a reference argument. It's a refrence to variable or another argument. They are declared by putting the `ref` keyword before the argument name, and can be used by passing a reference (done by putting the `ref` keyword before a variable or argument), for instance: ``` functi append(ref list, item) { # Appends item to list @@ -533,7 +533,7 @@ functi set_to_zero(ref i) { } let not_zero = 47; -set_to_zero(not_zero); +set_to_zero(ref not_zero); # Because the value was copied it's still not zero, so this prints "47" print(not_zero); ```