Difference between a "field" and a "mutable" variable

28 views
Skip to first unread message

Duke

unread,
Feb 19, 2013, 10:21:04 PM2/19/13
to nemer...@googlegroups.com
I'm new to Nemerle, OOP, and FP ...


However, I'm not grokking the difference between a "field" which can be declared "mutable", and a "mutable" variable??

For example:

mutable bar : int;

mutable bar = 1;

or are they the very same, with the former being the declaration of the latter??

NN

unread,
Feb 20, 2013, 2:54:32 AM2/20/13
to nemer...@googlegroups.com
Hello,

The concept is the same, the only question is where you have this variable.
It can be a local variable in function/method , then it exists only in this scope:

def f()
{
  mutable x = 1;
}

def g()
{
  x = 1; // Error, x exists only in local function f
}

On the other hand, field scope is class and all its methods:

class A
{
 mutable x : int;
 public F()
 {
   x = 1; // OK
   mutable y = 1; // Local variable , valid only in this method
 }
 public G()
 { 
   x= 2; // OK
   y = 2; // Error, y exists only in method F

Duke

unread,
Feb 20, 2013, 10:16:37 AM2/20/13
to nemer...@googlegroups.com


On Wednesday, February 20, 2013 12:54:32 AM UTC-7, NN wrote:
Hello,

The concept is the same, the only question is where you have this variable.
It can be a local variable in function/method , then it exists only in this scope:

[snip]
 
 I see!! So in summary (and to see if I really understand the syntax:

To declare a global variable - at the class level - I would use the "field" syntax:

mutable blah : int;

and initialize it later in a method.

To declare a local variable - at the method level - I would declare and initialize all at once:

mutable blah = 1;

NN

unread,
Feb 20, 2013, 3:41:43 PM2/20/13
to nemer...@googlegroups.com
Oh, I think you are new to OOP :)
You should read some articles about it.

I will start from beginning.

The full syntax for declaring local variables is following:

def <name> [: <type>] = <initializer>;
mutable <name> [: <type>] [= <initializer>];

inside [] is optional.

so you can write one of the following:

def a = 1; // This is same as below but compiler infers type
def a : int = 1; // Write time by ourselves

mutable a; // Initialized to default
mutable a : int; // Initialized to default but with type specified
mutable a = 0; // Same
mutable a : int = 0; // Same

As you can see compiler can also infer type from the usage:

mutable x;
x = "";

x.GetType() == "string"

This is the power of Nemerle :)

On the other hand, fields are part of the class, meaning part of the interface.
The decision that there is no type inference for fields (only in some special cases), because it can simply break others code.

So in fields you always specify type and you can also specify public/private/protected/internal access.

class A
{
 // This defines immutable fields
 public x; // Invalid
 public x : int; // OK
 public x : int = 10; // We can init here or in constructor

 // Mutable fields, same syntax but adding mutable before name
 mutable y : int;
}

The difference between mutable and 'def' (immutable) that you cannot change the value after you initialize it.

Example:
def a = 1;
a = 2; // Error

mutable a = 1;
a = 3; // OK

On the other hand each def creates new scope so you can write the following:

def a = 1;
def a = 2; // this 'a' is not same as first 'a'.

In Nemerle you should use more 'def' than 'mutable' , this helps you writing more maintainable and readable code.

Good luck with learning Nemerle. It is great language :)

Claude Normandin

unread,
Feb 20, 2013, 7:03:21 PM2/20/13
to nemer...@googlegroups.com
Thank you for the detailed explanation! I'm enjoying learning Nemerle more than a lot of other languages that I've tried!

--
duke
Reply all
Reply to author
Forward
0 new messages