Theoretically Stuck

Ulf Wiger (TN/EAB) <>
Sat Sep 2 22:01:29 CEST 2006


 
Rudolph van Graan wrote:
> 
> How do you write code that wraps the concept/data of a 
> person? A record with name and surname fields is the obvious answer:
> 
> -record(person,{name,surname}).
> 
>  From now on until forever, a person is a 3-tuple.
> 
> Now I want to make a version of person with an extra field, 
> say telephone number. Not all records have this. Some have a 
> work address as well.

This seems to be leading towards wanting structs, right?
A dynamically typed associative array.


> The first record type is already in 
> mnesia and I cannot change it, nor do I want to.

Of course, if you throw mnesia into the mix, you 
introduce the problem that most of the time you 
really _do_ want your data model to be strictly 
defined, all records having exactly the same fields.

You can, of course, store {Key, Value} tuples, and
have the Value structure vary between objects, but 
this will drastically reduce your ability to perform
efficient searches on the data, not to mention the 
ability to index the stuff.

(well - with 'rdbms', you _can_ still index it...;-)


> I push all my extra data into stuff, because it is still
> a person. This is not elegant. In Java I would have had
> no issues whatsoever.  

Yes you would, if you maintain the assumption that your 
going to store it in a database. What database would that 
be? I know there are object-oriented databases, but they
certainly have their problems too.


> So I make "versioned" or "namespaced" records by 
> parse transform. Now I cannot match anymore.

Did you do pattern matching in your Java program? (:

This is part of the problem, I think. You want dynamically
typed structs with efficient pattern matching, and of 
course, fast updates. And you want mnesia to support tables
of such objects. ets:select(), mnesia:select() and QLC
should have full support for them too, right?

I assure you, I would love to have that too.  (:



> Less critical but nuisance anyways is that in Erlang that I 
> cannot "inherit" or reuse code. I instinctively go into 
> grrrrrr mode when I see two functions in different places 
> that are the same. The same bug twice.

That should be reasonably easy to solve with a parse transform.
Yariv's Smerl supports a form of inheritance too, if I'm not
mistaken.

I'm not sure I agree with the "cannot reuse code" part.
I think there is a large degree of reuse in Erlang.
I think that OO programs can be difficult to reuse 
because the inheritance introduces so many implicit
dependencies - while you can achieve local reuse in
your own program, making generally reusable code is 
not nearly as easy.


> How can I make my_special_gen_server have all 
> the code automatically for a gen_server without me
> having to go lookup the callbacks every time and go
> into copy-and-paste mode?

How about writing your own behaviour extending gen_server?


BR,
Ulf W



More information about the erlang-questions mailing list