Another C# critique

  • Thread starter christopher diggins
  • Start date
M

Magnus Lidbom

(inline)



This is fairly obvious of course, but what about this: (taking your
classes)

//const supporting assembly
class Foo
{
private int m_value;
public static const Foo GetInstance(){return new Foo();}

public void CalculateNewValue()
{
m_value = Bar.NewRandomValue() * 10;
}

public int Value
{
//declare get const.
get const
{
return m_value;
}
set
{
m_value = value;
}
}
}

Now, CalculateNewValue obviously manipulates an instance of Foo's
value. However if you do not want that to happen, you should be able to
specify that.
I'm not sure what you mean. If you mean the ability to pass a Foo
reference around upon which CalculateNewValue cannot be called, then
thats just a const reference. This is a bit to obviuos though. I
doubt that's what you're getting at.
With the GetInstance() method this should be prevented. I
also can't declare CalculateNewValue() as const, because then I can never
call it (as I understand it ;)) because it would always alter the state of
the object. (if this is a misunderstanding, ignore my posting :))
Actually, you couild't compile the method. It modifies a non mutable
field, so it can't be const.

To determine if a call to CalculateNewValue() is violating the
contract in a non-const support language, the compiler has to examine
CalculateNewValue(),
No. Just it's declaration. This discussion is probably moot though. My
idea for validating IL from non supporting languages was flawed. See
the other subthread for details.

/Magnus Lidbom
 
F

Frans Bouma

Magnus Lidbom said:
I'm not sure what you mean. If you mean the ability to pass a Foo
reference around upon which CalculateNewValue cannot be called, then
thats just a const reference. This is a bit to obviuos though. I
doubt that's what you're getting at.

ok, I now see how stupid the idea was I wrote down :) (non-const
pointer to Foo is allowed to call CalculateNewValue, const pointer is not,
becuase it is not declared as const which is exactly what I want and can
be done at compile time indeed).

What I tried to illustrate is that a given class has N methods and
a subset of them should not be able to be called if the instance is
returned via a const method/property. If the language calling the class
instance doesn't support const, it's hard for teh compiler to determine
which methods are callable, i.e.: only those methods which are not
mutating the internal object, and without having every method not doing so
being declared as const by the language the class is written in.

FB
 
M

Magnus Lidbom

What I tried to illustrate is that a given class has N methods and
a subset of them should not be able to be called if the instance is
returned via a const method/property.
I'm assuming you mean a method returning a const reference. Whether
the method itself is const or not is not relevant.
If the language calling the class
instance doesn't support const, it's hard for teh compiler to determine
which methods are callable,
Unfortunately, I doubt that it could be done for non supporting
languages. However, a common const validator component, such as
discussed in another subthread, would make adding const to any
language very simple. Using a context dependent definition of simple
of course :)
i.e.: only those methods which are not
mutating the internal object, and without having every method not doing so
being declared as const by the language the class is written in.
I wouldn't expect it too examine non const methods to determine if
calling them might be safe. I'd expect it to disallow calling them
since calling them would be a breach of contract.


Regards /Magnus Lidbom
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

CSharp Coding Standards 18

Top