www.digitalmars.com         C & C++   DMDScript  

D - what about...

reply "Ivan Senji" <ivan.senji public.srce.hr> writes:
returning nonreference types by reference?
yesterday i wanted to write a function for a matrix class
so it could be used like this.

(for example:)

Matrix!(4) m = new Matrix!(4);
m(2,3) = 0;

but it is not possible becouse i can't return
(int or float) by reference

wouldn't it be nice if we could write:
inout int f(int x, int y){...}
or something like this?

Or is there some good reason why this is not possible?
Apr 09 2004
next sibling parent Russ Lewis <spamhole-2001-07-16 deming-os.org> writes:
Another way to do this would be to allow properties to have arguments on 
them.  Perhaps something like
	class Matrix {
		int m(int x,int y) { return ... }
		int m(int x,int y,int val) { ... }
	}
The problem here is that it's hard (maybe impossible?) to tell the 
difference between a function with 3 arguments and a property with two. 
  We could do a keyword, or maybe we could use a variatoin on the 
generics syntax:
	class Matrix {
		int m<int x,int y> () { return ... };
		int m<int x,int y> (int val) { ... }
	}
or evne:
	class Matrix {
		int m<int x,int y> {
			int get() { return ... };
			int set(int val) { ... };
		}
	}

Ivan Senji wrote:
 returning nonreference types by reference?
 yesterday i wanted to write a function for a matrix class
 so it could be used like this.
 
 (for example:)
 
 Matrix!(4) m = new Matrix!(4);
 m(2,3) = 0;
 
 but it is not possible becouse i can't return
 (int or float) by reference
 
 wouldn't it be nice if we could write:
 inout int f(int x, int y){...}
 or something like this?
 
 Or is there some good reason why this is not possible?

Apr 09 2004
prev sibling parent Brian Hammond <Brian_member pathlink.com> writes:
Here are a couple of short-term workarounds that you probably already know
about.

You could also have opCall return a pointer to the internal matrix cell that
you'd like to update for primitive types (int, float, etc).

E.g. T* opCall(uint r, uint c) { return &values[r][c]; }

used as 

*m(2,3) = 5.0;

Adds 1 character/token to the original.


You could use a lightweight wrapper object (think Java's Integer and Float
classes) in your matrix template class and have your matrix class' opCall return
an instance of one of these.  These classes would override '=' (actually it
appears that one cannot override '=') or provide an opCall.  These types of
classes are blemishes IMHO however.

T opCall(uint r, uint c) { return values[r][c]; }

used as 

m(2,3)(5.0);      // set matrix el (2,3) to 5.0

-Brian

In article <c5601m$1q7r$1 digitaldaemon.com>, Ivan Senji says...
returning nonreference types by reference?
yesterday i wanted to write a function for a matrix class
so it could be used like this.

(for example:)

Matrix!(4) m = new Matrix!(4);
m(2,3) = 0;

but it is not possible becouse i can't return
(int or float) by reference

wouldn't it be nice if we could write:
inout int f(int x, int y){...}
or something like this?

Or is there some good reason why this is not possible?

Apr 09 2004