{"id":334,"date":"2011-03-05T14:35:36","date_gmt":"2011-03-05T13:35:36","guid":{"rendered":"http:\/\/trigonakis.com\/blog\/?p=334"},"modified":"2011-03-14T22:44:39","modified_gmt":"2011-03-14T21:44:39","slug":"introduction-to-erlang-typing","status":"publish","type":"post","link":"http:\/\/trigonakis.com\/blog\/2011\/03\/05\/introduction-to-erlang-typing\/","title":{"rendered":"Introduction to Erlang : Typing"},"content":{"rendered":"<div class=\"seriesmeta\">This entry is part 3 of 16 in the series <a href=\"http:\/\/trigonakis.com\/blog\/series\/introduction-to-erlang\/\" class=\"series-57\" title=\"Introduction to Erlang\">Introduction to Erlang<\/a><\/div><p>In this post I will present you the <strong>data typing<\/strong> in Erlang.<\/p>\n<h3>Dynamic Typing<\/h3>\n<p>Erlang is a <strong>dynamic typing<\/strong> programming language. That means that when &#8220;declaring&#8221; a variable you do not need to statically specify the type it will be (as in <i>static typing<\/i> languages). Read more about dynamic typing <a href=\"https:\/\/secure.wikimedia.org\/wikipedia\/en\/wiki\/Dynamic_typing#Dynamic_typing\" target=\"_new\"> here<\/a>. For example, this is how you would declare and initialize an integer in C (static typing):<\/p>\n<pre lang=\"c\">\r\nint i = 17;\r\n<\/pre>\n<p>while in Erlang you would do: (we will see how to declare variables in a while)<\/p>\n<pre lang=\"erlang\">\r\nI = 17.\r\n<\/pre>\n<p>This approach has both advantages and disadvantages. For example, when programming, it is fast and convenient not to declare the variables&#8217; types, but in big projects it can lead to code readability problems unless well documented.<\/p>\n<h3>Variable Declaration<\/h3>\n<p>Erlang is heavily influnced by <i>Prolog<\/i>, so Prolog programmers should be ready to discover several commonalities. As with Prolog, the variable declaration is implicit; every character sequence generated by the regular expression <code>[A-Z_]([0-9a-zA-Z_]*)<\/code> is a variable. Some examples are <code>A, Ab, A_b, Ab1, _, _A_b3<\/code>. Just to mention that the ones starting with &#8220;<strong>_<\/strong>&#8221; has a special meaning; they are &#8220;<strong>don&#8217;t care<\/strong>&#8221; variables. When you use them, the value they will take will probably not be used later. Something like place keepers. You will meet them again and again, so don&#8217;t worry for now.<\/p>\n<h3>Variable Assignement<\/h3>\n<p>Another importand characteristic that Erlang inherited from Prolog is <strong>binding<\/strong> with pattern matching. In a nutshell, a value is not assigned to a variable, but bound with pattern matching. The most important consequence of this is that variables in Erlang are <strong>single assignement<\/strong>. Once bound to a value, their value cannot change for their lifetime. For example, in an emulator try the following:<\/p>\n<pre lang=\"erlang\">\r\n1> A = 10.\r\n10\r\n2> A = 11.\r\n<\/pre>\n<p>The output you will get is:<\/p>\n<pre>\r\n** exception error: no match of right hand side value 11\r\n<\/pre>\n<p>What happens is that <code>A<\/code> is bound to the value 10, so Erlang tries to pattern match 10 with the value 11, which is impossible.<\/p>\n<h3>Next<\/h3>\n<p>Since I want to keep the posts rather small, I will break this one here and I will present the actual Erlang types on the following post(s).<\/p>\n","protected":false},"excerpt":{"rendered":"<div class=\"seriesmeta\">This entry is part 3 of 16 in the series <a href=\"http:\/\/trigonakis.com\/blog\/series\/introduction-to-erlang\/\" class=\"series-57\" title=\"Introduction to Erlang\">Introduction to Erlang<\/a><\/div><p>In this post I will present you the data typing in Erlang. Dynamic Typing Erlang is a dynamic typing programming language. That means that when &#8220;declaring&#8221; a variable you do not need to statically specify the type it will be (as in static typing languages). Read more about dynamic typing here. For example, this is [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"jetpack_publicize_message":"","jetpack_is_tweetstorm":false,"jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","enabled":false}}},"categories":[40,51,28],"tags":[26,42,49],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p1ouW6-5o","_links":{"self":[{"href":"http:\/\/trigonakis.com\/blog\/wp-json\/wp\/v2\/posts\/334"}],"collection":[{"href":"http:\/\/trigonakis.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/trigonakis.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/trigonakis.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/trigonakis.com\/blog\/wp-json\/wp\/v2\/comments?post=334"}],"version-history":[{"count":18,"href":"http:\/\/trigonakis.com\/blog\/wp-json\/wp\/v2\/posts\/334\/revisions"}],"predecessor-version":[{"id":417,"href":"http:\/\/trigonakis.com\/blog\/wp-json\/wp\/v2\/posts\/334\/revisions\/417"}],"wp:attachment":[{"href":"http:\/\/trigonakis.com\/blog\/wp-json\/wp\/v2\/media?parent=334"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/trigonakis.com\/blog\/wp-json\/wp\/v2\/categories?post=334"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/trigonakis.com\/blog\/wp-json\/wp\/v2\/tags?post=334"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}