Re: Simplicity

From: Ioannis Vranos (ivr_at_guesswh.at.grad.com)
Date: 09/04/04


Date: Sat, 04 Sep 2004 23:46:09 +0300

JKop wrote:
> The other day I had to write some code to manipulate a Micrsoft Excel
> spread***. I had to write it in Visual Basic, which I haven't used for
> about 5 years. Anyway, it slowly started creeping back to me and I began to
> understand terms like:
>
> ByVal (by value)
> ByRef (by reference)
>
> I remember how excited I was switching to C++, working with a better, more
> controllable language.
>
> But now going back and using VB I've realized how much I love the
> simplicity. One thing I particularly like is re-seatable references:
>
> Dim k as Zone
>
> The above is an object definition. "k" is the name of the object and "Zone"
> is the type. It would be akin to:
>
> Zone k;
>
> in C++. Anyway, while in C++, that would create an object, all it does in VB
> is create a re-seatable reference. For example, if I try to do this:
>
> Dim k as Zone
> k.SomeMemberFunction
>
> Then there will be a runtime error, it would be like doing the following in
> C++:
>
>
> Zone* k;
> k->SomeMemberFunction();
>
>
> Here's how you actually work with it:
>
> Dim k as Zone
> Set k = New Zone
> k.SomeMemberFunction

In the above it looks like you create an object in the free store.

> Anyway, I love the idea of re-seatable references. Sure, we can re-seat
> pointers, but then that adds all the bull*** of dodgy syntax.

I do not understand what you mean by re-seatable references, but I
suspect you are using .NET and you are talking about .NET (CLI) features.

The address of reference objects in C++/CLI handles (and the current
managed extensions pointers) is not the same but changes as the runtime
   repositions objects in the managed heap.

In C++/CLI the address of operator is % which returns a "tracking
reference".

Check this page of mine:

http://www23.brinkster.com/noicys/cppcli.htm

And the current thread with the stupid title:

"Why not develop new language"

In any case I haven't understood what exactly you like, the run-time error?!

>
> So anyway, I'd just like to hear general opinions on the simplicity in other
> languages. I particulary like how VB uses actual words instead of symbols,
> eg.:
>
> //start VB code
>
> Public Sub DoStuff(ByRef r as integer)
>
> r = 12
>
> End Function
>
> //end VB code
>
>
> would be the equivalent of:
>
>
> void DoStuff(int &r)
> {
> r = 12
> }

How can you do this in VB at compile-time, which also produces 100% pure
IL code?

//Managed object
ref class test
{
  int value;

  public:
  test() { value=1; }

  // Trivial property - Compiler generated definition
  property int Value;
};

template <class T>
void multiply2(T %obj)
{
  obj.Value=2;
}

int main()
{
  // Managed object with stack semantics
  //Deterministic destruction
  test someobj;

  multiply2(someobj);

  System::Console::WriteLine(someobj.Value);

}

C:\c>cl /clr:safe temp.cpp
Microsoft (R) C/C++ Optimizing Compiler Version 14.00.40809
for Microsoft (R) .NET Framework version 2.00.40607.16
Copyright (C) Microsoft Corporation. All rights reserved.

temp.cpp
Microsoft (R) Incremental Linker Version 8.00.40809
Copyright (C) Microsoft Corporation. All rights reserved.

/out:temp.exe
temp.obj

C:\c>temp
2

C:\c>

> Before I go and write one myself, has anyone written a class for a re-
> seatable reference? I think I'll use macros to achieve syntax something
> like:
>
> int% a; //a re-seatable reference
>
> int k;
>
> Set a = k;
>
> a = 4; //changes k's value
>
> int z;
>
> Set a = z;
>
> a = 9; //changes z's value

Since you have pointers(and C++/CLI handles), you can do exactly the
same operations. So you insist on a different syntax?

You can write a simple template then. Something like:

template <class T>
ref class RTrackRef
{
  T ^h;

  public:
  RTrackRef() { h=nullptr; }
  RTrackRef(T ^newHandleValue)
  {
    h= newHandleValue;
  }

  operator T() { return *h; }
  operator T ^() { return h; }

  T ^ operator=(T ^newHandleValue)
  {
    h= newHandleValue;

    return h;
  }

  const T % operator=(const T %newValue)
  {
    *h = newValue;

    return *h;
  }
};

template <class T>
ref class NTrackRef
{
  T *p;

  public:
  NTrackRef() { p=0; }
  NTrackRef(T *newPointerValue)
  {
    p= newPointerValue;
  }

  operator T() { return *p; }
  operator T *() { return p; }

  T * operator=(T *newPointerValue)
  {
    p= newPointerValue;

    return p;
  }

  const T & operator=(const T &newValue)
  {
    *p = newValue;

    return *p;
  }
};

int main()
{
  using namespace System;

  NTrackRef<int> a; //a re-seatable reference

  int k;

  a = &k;

  a = 4; //changes k's value

  int z;

  a = &z;

  a = 9; //changes z's value

}

NTrackRef is for native types and RTrackRef for CLI reference types.

Regards,

Ioannis Vranos

http://www23.brinkster.com/noicys