git.iko.soy/rowtype-yoga/purescript-native
on golang help sign in
Merge pull request #259 from timmytofu/patch-2

Add an example of object creation with syntax

commit62b500a5272950cb34dccc113da65350d0ec7f7e authorPhil Freeman <paf31@cantab.net> dateFri 7 Mar 2014, 18:33 (13 years ago) parentd046eecdc01fc5
1 file changed+11
docs/source/intro.rst+11
docs/source/intro.rst+11
@@ -102,6 +102,10 @@ The following code defines a `Person` data type and a function to generate a str
102102
103103 showPerson :: Person -> String
104104 showPerson (Person o) = o.name ++ ", aged " ++ numberToString(o.age)
105+
106+ examplePerson :: Person
107+ examplePerson = Person {name: "Bonnie", age: 26}
108+
105109
106110Line by line, this reads as follows:
107111
@@ -110,6 +114,7 @@ Line by line, this reads as follows:
110114- The `numberToString` function is written in Javascript, and converts a `Number` to its `String` representation
111115- The `showPerson` function takes a `Person` and returns a `String`
112116- `showPerson` works by case analysis on its argument, first matching the constructor `Person` and then using string concatenation and object accessors to return its result.
117+- `examplePerson` is a Person object, made with the Person constructor and given the String "Bonnie" for the name value and the Number 26 for the age value.
113118
114119The generated Javascript looks like this::
115120
@@ -120,6 +125,12 @@ The generated Javascript looks like this::
120125 function showPerson(_1) {
121126 return _1.values[0].name + ", aged " + numberToString(_1.values[0].age);
122127 };
128+
129+ var examplePerson = Person({
130+ name: "Bonnie",
131+ age: 26
132+ });
133+
123134
124135Related Projects
125136----------------