The way of accessing record's attributes.

classic Classic list List threaded Threaded
8 messages Options
Reply | Threaded
Open this post in threaded view
|

The way of accessing record's attributes.

Alex Feng
Hi,

Does anyone know why erlang has to attach the record's name to be able to access an attribute ?
I don't understand, for example, the record "#robot" has been assigned to variable "Crusher", why do we have to use  "Crusher#robot.hobbies" instead of "Crusher.hobbies" ? 


5> Crusher = #robot{name="Crusher", hobbies=["Crushing people","petting cats"]}.
#robot{name = "Crusher",type = industrial,
hobbies = ["Crushing people","petting cats"],
details = []}
6> Crusher#robot.hobbies.
["Crushing people","petting cats"]


Br,
Alex

_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions
Reply | Threaded
Open this post in threaded view
|

Re: The way of accessing record's attributes.

Stanislaw Klekot
On Tue, Nov 08, 2016 at 02:12:35PM +0100, Alex Feng wrote:
> Does anyone know why erlang has to attach the record's name to be able to
> access an attribute ?
> I don't understand, for example, the record "#robot" has been assigned to
> variable "Crusher", why do we have to use  "Crusher#robot.hobbies" instead
> of "Crusher.hobbies" ?

Records are syntax sugar for tuples. Compiler doesn't know what kind of
value is in `Crusher' (maybe some different record) and what are the
field names (which is more important), so you need to specify
appropriate record name yourself.

--
Stanislaw Klekot
_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions
Reply | Threaded
Open this post in threaded view
|

Re: The way of accessing record's attributes.

Alex S.
In reply to this post by Alex Feng
Potentially, nothing stops you from having multiple records with same-named fields, and as erlang isn’t statically-typed, compiler wouldn’t have a way to tell those apart.
That said, you could potentially add support for such a syntax so long as it’s unambiguous.

_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions
Reply | Threaded
Open this post in threaded view
|

Re: The way of accessing record's attributes.

Hugo Mills-2
In reply to this post by Alex Feng
On Tue, Nov 08, 2016 at 02:12:35PM +0100, Alex Feng wrote:
> Hi,
>
> Does anyone know why erlang has to attach the record's name to be able to
> access an attribute ?
> I don't understand, for example, the record "#robot" has been assigned to
> variable "Crusher", why do we have to use  "Crusher#robot.hobbies" instead
> of "Crusher.hobbies" ?

   First, note that records are a *compile time* construct. Once the
code is compiled, it turns a record into a tagged tuple (e.g. {robot,
"Crusher", industrial, [], []}), and turns all of the access of the
record into pattern matches like {robot, _, industrial, _, _}, or
simple element extraction with element/2.

   In order to do this, the compiler needs to know what type of record
it is, *at compile time*. In your example, it's pretty obvious what
kind of record the variable is, but if you are (say) passing that
variable to a function, how would the compiler know which record
definition it should be using? It can't find all of the call sites
where the function is called from, because some of them might be
outside the module being compiled...

   Hugo.

>
> 5> Crusher = #robot{name="Crusher", hobbies=["Crushing people","petting
> cats"]}.
> #robot{name = "Crusher",type = industrial,
> hobbies = ["Crushing people","petting cats"],
> details = []}
> 6> Crusher#robot.hobbies.
> ["Crushing people","petting cats"]
>
>
> Br,
> Alex

> _______________________________________________
> erlang-questions mailing list
> [hidden email]
> http://erlang.org/mailman/listinfo/erlang-questions


--
Hugo Mills             | I thought I'd discovered a new colour, but it was
hugo@... carfax.org.uk | just a pigment of my imagination.
http://carfax.org.uk/  |
PGP: E2AB1DE4          |

_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions

signature.asc (853 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: The way of accessing record's attributes.

Raimo Niskanen-2
In reply to this post by Alex Feng
On Tue, Nov 08, 2016 at 02:12:35PM +0100, Alex Feng wrote:
> Hi,
>
> Does anyone know why erlang has to attach the record's name to be able to
> access an attribute ?
> I don't understand, for example, the record "#robot" has been assigned to
> variable "Crusher", why do we have to use  "Crusher#robot.hobbies" instead
> of "Crusher.hobbies" ?

Records is a compile time syntactical sugar on tagged tuples.

So "Crusher#robot.hobbies" is translated to something like
  begin robot = element(1, Crusher), element(3, Crusher) end
if 'hobbies' is the second element of #robot.

The compiler can not know that it is the hobbies field from the #robot
record you want to extract unless told so.  You may mean the hobbies field
from the #whatever record.  It could see that it is a #robot record from an
earlier assignment in the same code and warn about it if it's not,
but in the general case it can not know what is stored in the variable
hence leaves it to be solved in runtime.


>
>
> 5> Crusher = #robot{name="Crusher", hobbies=["Crushing people","petting
> cats"]}.
> #robot{name = "Crusher",type = industrial,
> hobbies = ["Crushing people","petting cats"],
> details = []}
> 6> Crusher#robot.hobbies.
> ["Crushing people","petting cats"]
>
>
> Br,
> Alex

> _______________________________________________
> erlang-questions mailing list
> [hidden email]
> http://erlang.org/mailman/listinfo/erlang-questions


--

/ Raimo Niskanen, Erlang/OTP, Ericsson AB
_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions
Reply | Threaded
Open this post in threaded view
|

Re: The way of accessing record's attributes.

Alex Feng


Thank you all for the detailed explanation, I guess I was trying to use  "C/C++" way of thinking to understand <record>. 

Br,
Alex

2016-11-08 15:41 GMT+01:00 Raimo Niskanen <[hidden email]>:
On Tue, Nov 08, 2016 at 02:12:35PM +0100, Alex Feng wrote:
> Hi,
>
> Does anyone know why erlang has to attach the record's name to be able to
> access an attribute ?
> I don't understand, for example, the record "#robot" has been assigned to
> variable "Crusher", why do we have to use  "Crusher#robot.hobbies" instead
> of "Crusher.hobbies" ?

Records is a compile time syntactical sugar on tagged tuples.

So "Crusher#robot.hobbies" is translated to something like
  begin robot = element(1, Crusher), element(3, Crusher) end
if 'hobbies' is the second element of #robot.

The compiler can not know that it is the hobbies field from the #robot
record you want to extract unless told so.  You may mean the hobbies field
from the #whatever record.  It could see that it is a #robot record from an
earlier assignment in the same code and warn about it if it's not,
but in the general case it can not know what is stored in the variable
hence leaves it to be solved in runtime.


>
>
> 5> Crusher = #robot{name="Crusher", hobbies=["Crushing people","petting
> cats"]}.
> #robot{name = "Crusher",type = industrial,
> hobbies = ["Crushing people","petting cats"],
> details = []}
> 6> Crusher#robot.hobbies.
> ["Crushing people","petting cats"]
>
>
> Br,
> Alex

> _______________________________________________
> erlang-questions mailing list
> [hidden email]
> http://erlang.org/mailman/listinfo/erlang-questions


--

/ Raimo Niskanen, Erlang/OTP, Ericsson AB
_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions


_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions
Reply | Threaded
Open this post in threaded view
|

Re: The way of accessing record's attributes.

Raimo Niskanen-2
On Tue, Nov 08, 2016 at 04:21:59PM +0100, Alex Feng wrote:
> Thank you all for the detailed explanation, I guess I was trying to use
>  "C/C++" way of thinking to understand <record>.

This corresponds to the case in C where you have a void *, then you have to
cast it to the appropriate (struct robot *) before accessing a field
->hobbies in it.

In Erlang you can not declare the type of a variable, which seems to be the
C/C++ thinking you fell into.  Type is a run-time property in Erlang.

(except when using the static type checker Dialyzer)


>
> Br,
> Alex
>
> 2016-11-08 15:41 GMT+01:00 Raimo Niskanen <
> [hidden email]>:
>
> > On Tue, Nov 08, 2016 at 02:12:35PM +0100, Alex Feng wrote:
> > > Hi,
> > >
> > > Does anyone know why erlang has to attach the record's name to be able to
> > > access an attribute ?
> > > I don't understand, for example, the record "#robot" has been assigned to
> > > variable "Crusher", why do we have to use  "Crusher#robot.hobbies"
> > instead
> > > of "Crusher.hobbies" ?
> >
> > Records is a compile time syntactical sugar on tagged tuples.
> >
> > So "Crusher#robot.hobbies" is translated to something like
> >   begin robot = element(1, Crusher), element(3, Crusher) end
> > if 'hobbies' is the second element of #robot.
> >
> > The compiler can not know that it is the hobbies field from the #robot
> > record you want to extract unless told so.  You may mean the hobbies field
> > from the #whatever record.  It could see that it is a #robot record from an
> > earlier assignment in the same code and warn about it if it's not,
> > but in the general case it can not know what is stored in the variable
> > hence leaves it to be solved in runtime.
> >
> >
> > >
> > >
> > > 5> Crusher = #robot{name="Crusher", hobbies=["Crushing people","petting
> > > cats"]}.
> > > #robot{name = "Crusher",type = industrial,
> > > hobbies = ["Crushing people","petting cats"],
> > > details = []}
> > > 6> Crusher#robot.hobbies.
> > > ["Crushing people","petting cats"]
> > >
> > >
> > > Br,
> > > Alex
> >
> > > _______________________________________________
> > > erlang-questions mailing list
> > > [hidden email]
> > > http://erlang.org/mailman/listinfo/erlang-questions
> >
> >
> > --
> >
> > / Raimo Niskanen, Erlang/OTP, Ericsson AB
> > _______________________________________________
> > erlang-questions mailing list
> > [hidden email]
> > http://erlang.org/mailman/listinfo/erlang-questions
> >

> _______________________________________________
> erlang-questions mailing list
> [hidden email]
> http://erlang.org/mailman/listinfo/erlang-questions


--

/ Raimo Niskanen, Erlang/OTP, Ericsson AB
_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions
Reply | Threaded
Open this post in threaded view
|

Re: The way of accessing record's attributes.

Richard A. O'Keefe-2


> On Tue, Nov 08, 2016 at 04:21:59PM +0100, Alex Feng wrote:
>> Thank you all for the detailed explanation, I guess I was trying to use
>>  "C/C++" way of thinking to understand <record>.

For what it's worth, you may have noticed that a lot of structs
in the UNIX api have prefixes on their fields, e.g.,
tm_sec, tm_min, tm_hour, or st_dev, st_ino, st_mode, ...
The reason is simple: there was a time before C got typed
pointers (indeed, when it just barely had types at all),
so the prefixes were necessary for disambiguation in the very
same way.

The "C way of thinking" hasn't always been the same.
_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions