maandag 11 oktober 2010

difference between clone and assign

Assigning one primitive to another copies the value from one instance to the other.
However, if you create a variable and assign it an aggregate value, then you create a second variable and you assign the first variable to it, then all changes to the second variable are also reflected in the first variable. In other words, assigning an aggregate from one variable to another assigns the pointer.
a = 3;
b = a;
++b;

assert a == 3;
assert b == 4;

s1 = "aaaa";
s2 = s1;
s2 = 'bbbb';
assert s1 == 'aaaa';

m = { x=1 };
n = m;
assert n.x == 1;
n.x = 2;
assert m.x == 2;
assert n.x == 2;

a1 = [1,2,3];
a2 = a1;
a2[1] = 35;
assert a1[1] == 35;

I'm just wondering if this is correct. This behavior is similar to Java and probably some other languages, but do I want this ?
Perl has a lexical solution: you copy either values or pointers to objects:
my %a = (k1=>3);
my %b = %a; #clone
$b{k1} = 7;
print "$a{k1}\n"; #prints 3

my $c = \%a; #copy
$c->{k1} = 35;
print "$a{k1}\n"; #prints 35

pondering mdsl

A couple of things that keep me occupied.
What if I would introduce classes ? I'd probably have to get rid of maps. Because what would be the added value of maps? None as far as I can see.
I could then also introduce the idea that everything is a class. Currently, if you have a primitive and you assign a function to a member of it, then the original variable turns from a primitive into a map.
a = 3;
a.f = function(x,y){
  return x+y; 
}
However, if everything were a class, then the function assignment would just extend the class of the primitive. Or would it ?
How does that work in java ? Primitives can both have value and methods.
That also raises the question of the 'this' object. Make 'this' a reserved word ? Or 'self' ?
Inheritance is out of the question for now. I'm thinking of introducing a 'clone' operator (something like '<=='). Then you can use the clone of an object to extend it further.
c = class {
  def a,b,c;
  def new = function(){
    c.a = 0;
    c.b = 0;
    c.c = 0;
  }
}
d <== c;
d.sum = function(){
  return d.a + d.b + d.c;
}
Looks like we're not really having classes here, but only instantiations, or objects. That's OK. It also looks like we do not need a 'this' thing.

zondag 10 oktober 2010

Yo

Wat is een botkop ? Volgens mij:
  • een kerel die rondloopt met een bot op zijn kop
  • een kerel met een botte kop
  • een kerel met meer bot dan hersenen in zijn kop
 Foto werd genomen door mijn madam in 2001 in een berm ergens in Oezbekistan, tijdens een fietstocht van BelgiĆ« naar Cambodja.

Deze blog is voorlopig enkel een stub van dingen waar ik mee bezig ben.
  • MDSL: Minimalistic Domain Specific Language