Skip to content

Commit

Permalink
Move Ref type + basic test
Browse files Browse the repository at this point in the history
  • Loading branch information
hubertp committed Apr 4, 2022
1 parent 6afa443 commit 02ab5b1
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 53 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
x ->
result = Builtins.Ref.new '{ message: ""}'
result = Ref.new '{ message: ""}'
x.catch err->
message = err.to_display_text
Builtins.Ref.put result ('{ "kind": "Dataflow", "message": ' + message.to_json.to_text + '}')
Builtins.Ref.get result
Ref.put result ('{ "kind": "Dataflow", "message": ' + message.to_json.to_text + '}')
Ref.get result

45 changes: 45 additions & 0 deletions distribution/lib/Standard/Base/0.0.0-dev/src/Data/Ref.enso
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
## Utilities for working with mutable references.
type Ref

## A mutable reference type.
@Builtin_Type
type Ref

## FIXME consider moving this as methods of Ref, currently a lot of stuff depends
## on the current design
## Gets the contents of the mutable reference ref.

Arguments:
- ref: The reference to get the contents of.

> Example
Getting the contents of a reference.

Ref.get (Ref.new 0)
get : Ref -> Any
get ref = @Builtin_Method "Ref.get"

## Puts a new value into the reference, returning the old value.

Arguments:
- ref: The reference in which to store the value.
- new_value: The new value to store in ref.

> Example
Storing the value 10 in a reference.

Ref.put (Ref.new 0) 10
put : Ref -> Any -> Any
put ref new_value = @Builtin_Method "Ref.put"

## Creates a new reference containing the provided value.

Arguments:
- value: The value to be contained in the ref.

> Example
Creating a new reference containing the value 7.

Ref.new 7
new : Any -> Ref
new value = @Builtin_Method "Ref.new"
2 changes: 2 additions & 0 deletions distribution/lib/Standard/Base/0.0.0-dev/src/Main.enso
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import project.Data.Ordering
import project.Data.Ordering.Sort_Order
import project.Data.Pair
import project.Data.Range
import project.Data.Ref
import project.Data.Text.Extensions
import project.Data.Text.Matching
import project.Data.Vector
Expand Down Expand Up @@ -53,6 +54,7 @@ from project.Data.Number.Extensions export all hiding Math, String, Double
from project.Data.Noise export all hiding Noise
from project.Data.Pair export Pair
from project.Data.Range export Range
from project.Data.Ref export Ref
## TODO [RW] Once autoscoping is implemented or automatic imports for ADTs are
fixed in the IDE, we should revisit if we want to export ADTs like `Case` by
default. It may be unnecessary pollution of scope, but until the issues are
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,9 @@ public class Mutable {
*/
public Mutable(Builtins builtins, Language language, ModuleScope scope) {
array = null;
ref = null;
this.builtins = builtins;

ref = new AtomConstructor("Ref", scope).initializeFields();
scope.registerConstructor(ref);
scope.registerMethod(ref, "new", NewRefMethodGen.makeFunction(language));
scope.registerMethod(ref, "get", GetRefMethodGen.makeFunction(language));
scope.registerMethod(ref, "put", PutRefMethodGen.makeFunction(language));
}

/** @return the Array constructor. */
Expand All @@ -35,6 +31,6 @@ public AtomConstructor array() {

/** @return the Ref constructor. */
public AtomConstructor ref() {
return ref;
return builtins.getBuiltinType("Ref");
}
}
44 changes: 0 additions & 44 deletions engine/runtime/src/main/resources/Builtins.enso
Original file line number Diff line number Diff line change
Expand Up @@ -741,50 +741,6 @@ type Meta
get_qualified_type_name : Any -> Text
get_qualified_type_name value = @Builtin_Method "Meta.get_qualified_type_name"

## Utilities for working with mutable references.
type Ref

## A mutable reference type.
@Builtin_Type
type Ref

## Creates a new reference containing the provided value.

Arguments:
- value: The value to be contained in the ref.

> Example
Creating a new reference containing the value 7.

Ref.new 7
new : Any -> Ref
new value = @Builtin_Method "Ref.new"

## Gets the contents of the mutable reference ref.

Arguments:
- ref: The reference to get the contents of.

> Example
Getting the contents of a reference.

Ref.get (Ref.new 0)
get : Ref -> Any
get ref = @Builtin_Method "Ref.get"

## Puts a new value into the reference, returning the old value.

Arguments:
- ref: The reference in which to store the value.
- new_value: The new value to store in ref.

> Example
Storing the value 10 in a reference.

Ref.put (Ref.new 0) 10
put : Ref -> Any -> Any
put ref new_value = @Builtin_Method "Ref.put"

## The root type of the Enso numeric hierarchy.

If a Number is expected, then the program can provide either a Decimal or
Expand Down
12 changes: 12 additions & 0 deletions test/Tests/src/Data/Ref_Spec.enso
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
from Standard.Base import all

import Standard.Test

spec = Test.group "Refs" <|
Test.specify "should be able to store and retrieve value in references" <|
r = Ref.new 'foo'
Ref.put r 'bar'
v = Ref.get r
v.should_equal 'bar'

main = Test.Suite.run_main here.spec

0 comments on commit 02ab5b1

Please sign in to comment.