Literal format for the Map type #6167
Replies: 4 comments 12 replies
-
The old format was accidental and we never designed it properly. I believe that the old syntax does not fit the current Enso language design. I do not know yet what design is correct though. I think something like
Anyway, this needs IMO a little bit more discussion and brainstorming. I'd love to hear what you think about these ideas. |
Beta Was this translation helpful? Give feedback.
-
The curly brace felt natural to me as it is very similar to python (although python uses We use square brackets exclusively for Vector literals so having a similar unique marker for brackets feels in keeping. The macro syntax would be a new thing we haven't had before (or at least not such that an average user would know). |
Beta Was this translation helpful? Give feedback.
-
If we shall go with the "block based syntax", then we don't need any parser changes at all. This diff implements such syntax fully in Enso standard libraries: enso$ git diff
diff --git distribution/lib/Standard/Base/0.0.0-dev/src/Data/Map.enso distribution/lib/Standard/Base/0.0.0-dev/src/Data/Map.enso
index ad0a4a85f1..f684e1c88b 100644
--- distribution/lib/Standard/Base/0.0.0-dev/src/Data/Map.enso
+++ distribution/lib/Standard/Base/0.0.0-dev/src/Data/Map.enso
@@ -5,6 +5,8 @@ import project.Data.Text.Extensions
import project.Data.Text.Text
import project.Errors.Illegal_Argument.Illegal_Argument
import project.Errors.No_Such_Key.No_Such_Key
+import project.Runtime.State
+import project.Runtime.Ref.Ref
from project.Data.Boolean import Boolean, True, False
from project import Error, Nothing, Any, Panic
@@ -23,6 +25,12 @@ from project import Error, Nothing, Any, Panic
treated as a Map.
@Builtin_Type
type Map key value
+ make : Any -> Map
+ make ~action =
+ ref = Ref.new Map.empty
+ State.run Map ref action
+ ref.get
+
## Returns an empty map.
empty : Map
empty = @Builtin_Method "Map.empty"
@@ -357,3 +365,9 @@ type Map key value
## PRIVATE
get_builtin : Any -> Any -> Any
get_builtin self key ~if_missing = @Builtin_Method "Map.get_builtin"
+
+Any.$ self that =
+ ref = State.get Map
+ map = ref.get
+ new_map = map.insert self that
+ ref.put new_map
diff --git test/Tests/src/Data/Map_Spec.enso test/Tests/src/Data/Map_Spec.enso
index 64975c0676..3b3f84348f 100644
--- test/Tests/src/Data/Map_Spec.enso
+++ test/Tests/src/Data/Map_Spec.enso
@@ -359,6 +359,13 @@ spec =
m2.to_vector.sort on=_.first . should_equal [["A", 1], ["B", 2]]
m3.to_vector.sort on=_.first . should_equal [["A", 1], ["C", 3]]
+ Test.specify "nicer syntax for maps" <|
+ m1 = Map.make <|
+ "A" $ 1
+ "B" $ 2
+ "C" $ 3
+ m1.to_vector.sort on=_.first . should_equal [["A", 1], ["B", 2], ["C", 3]]
+
Test.specify "should handle inserts with same keys (1)" <|
m1 = Map.singleton "A" 1
m2 = m1.insert "A" 2 one can probably use other symbol instead of |
Beta Was this translation helpful? Give feedback.
-
Expanding on this ticket and bring it back to life... I think the multiline map syntax, would not work well within the node editing style we have (though would with a vertical editor like proposed for Vector). The brackets and inline style would be clear for users and render simply in the ide. I think @wdanilo simplified cc: @sylwiabr |
Beta Was this translation helpful? Give feedback.
-
As we now have a built-in Map type we would like to add a literal format to create a map instance.
There appears to have been old support within the old parser for a syntax:
enso/engine/runtime/src/test/scala/org/enso/compiler/test/pass/analyse/DataflowAnalysisTest.scala
Line 1295 in d1c52fe
The format appears to have been:
{ x := 1; b := 2 }
.Readding support to this for the parser would be the first step, and then making the engine produce a Map from it.
For example:
{ x := 1; b := 2}
would be translated toMap.empty.insert a 1 . insert b 2
{ "key" := 1, "value" := 2}
would be translated toMap.empty.insert "key" 1 . insert "value" 2
A ticket for the parser has been added by @kazcw: #6162
Is everyone happy on adding this back in?
I would suggest switching to
,
as the delimiter rather than;
as appears to be the case in the old parser.cc: @hubertp @Akirathan @JaroslavTulach @wdanilo @radeusgd @GregoryTravis
Beta Was this translation helpful? Give feedback.
All reactions