Any difference between const Class& and Class const&? - reference

Is there any difference between that expressions :
const Class&
Class const&
for example, when those are parameters of function ?

There is no difference between const Class& and Class const&; similarly here is no difference between const Class* and Class const*.
And so,
void f1 (const Class& c)
and
void f1 (Class const& c)
are interchangeable with no difference.
Both versions denote a reference to a constant Class instance and can be used interchangeably.

Related

How to invoke a kotlin function when given the function name as a string?

I'm making a project with many functions, each named prx, x being a number up to 200, is it possible to create a call from a string like this
var ="pr" + x
You can do this using reflection
class MyTestClass {
fun pr1(): Int = 5
fun pr2(value: Int) = value.toString()
}
class SomeOtherClass{
fun main(){
val myTestClassObj = MyTestClass()
// Find pr1 by name (function without arguments)
val pr1 = myTestClassObj::class.java.getMethod("pr1").kotlinFunction
// call pr1, pass class object as argument, used to call the function
val pr1Result = pr1?.call(myTestClassObj)
// Find function with arguments, pass function name and type of arguments
val pr2 = MyTestClass::class.java.getMethod("pr2", Int::class.java).kotlinFunction
// Call pr2, pass class reference and the function parameter
val pr2Result = pr2?.call(myTestClassObj, 100)
}
}
Be carefull when working with reflection, its easy to create untraceable bugs using it and often it is only a workaround for bad design.

Why C# doesn't support overloading based on return type

In C# why can't we have two functions with same signature except return type:
(1) int Function(int a,int b)
{
---}
(2) string Function(int a,int b)
{
---}
Why C# doesn't support overloading based on return type?
object result = Function(a, b);
Which one do you call?
Because you can't specify the return type when you call it.
// int or string?
Function(a, b);
I'm also curious why you would want to do this, naming something the same but returning two different things is probably a bad idea. This code is far more readable and the intent is clearer:
string x = FunctionToString(a, b);
int y = FunctionToInt(a, b);

How to instantiate a class using an explicit first, and a default second argument, via the meta model?

I have the following toplevel class:
class Example(shared String first = "default one", shared String second = "default two") {
}
Now, I want to instantiate this class using an explicit value for first, but the default value for second.
I know how to do this via explicitly compiled code, by using named arguments:
void instantiateExample(String firstValue) {
Example ex = Example { first = firstValue; };
assert(ex.first == firstValue);
assert(ex.second == "default two");
}
Now, I would like to do the same thing as the above code, but by using the Class<Example, []|[String,String=]> object.
with class model it is ...
Class<Example,[]|[String, String=]> exampleModel = `Example`;
value e1 = exampleModel();
value e2 = exampleModel("foo");
value e3 = exampleModel("foo", "bar");
or with class declaration it is ...
ClassDeclaration exampleDeclaration = `class Example`;
assert(is Example e1 = exampleDeclaration.instantiate());
assert(is Example e2 = exampleDeclaration.instantiate([], "foo"));
assert(is Example e3 = exampleDeclaration.instantiate([], "foo", "bar"));
HTH

How do I use groovy's AS keyword

This may be a duplicate but "as" is an INCREDABLY hard keyword to google, even S.O. ignores "as" as part of query.
So I'm wondering how to implement a class that supports "as" reflexively. For an example class:
class X {
private val
public X(def v) {
val=v
}
public asType(Class c) {
if (c == Integer.class)
return val as Integer
if(c == String.class)
return val as String
}
}
This allows something like:
new X(3) as String
to work, but doesn't help with:
3 as X
I probably have to attach/modify the "asType" on String and Integer somehow, but I feel any changes like this should be confined to the "X" class... Can the X class either implement a method like:
X fromObject(object)
or somehow modify the String/Integer class from within X. This seems tough since it won't execute any code in X until X is actually used... what if my first usage of X is "3 as X", will X get a chance to override Integer's asType before Groovy tries to call is?
As you say, it's not going to be easy to change the asType method for Integer to accept X as a new type of transformation (especially without destroying the existing functionality).
The best I can think of is to do:
Integer.metaClass.toX = { -> new X( delegate ) }
And then you can call:
3.toX()
I can't think how 3 as X could be done -- as you say, the other way; new X('3') as Integer is relatively easy.
Actually, you can do this:
// Get a handle on the old `asType` method for Integer
def oldAsType = Integer.metaClass.getMetaMethod( "asType", [Class] as Class[] )
// Then write our own
Integer.metaClass.asType = { Class c ->
if( c == X ) {
new X( delegate )
}
else {
// if it's not an X, call the original
oldAsType.invoke( delegate, c )
}
}
3 as X
This keeps the functionality out of the Integer type, and minimizes scope of the effect (which is good or bad depending on what you're looking for).
This category will apply asType from the Integer side.
class IntegerCategory {
static Object asType(Integer inty, Class c) {
if(c == X) return new X(inty)
else return inty.asType(c)
}
}
use (IntegerCategory) {
(3 as X) instanceof X
}

How should one best recode this example extension method to be generic for all numeric types?

How should one best recode this example extension method to be generic for all numeric types?
public static float clip(this float v, float lo, float hi)
{ return Math.Max(lo, Math.Min(hi, v)); }
Thanks.
// IComparable constraint for numeric types like int and float that implement IComparable
public static T clip<T>(this T v, T lo, T hi) where T : IComparable<T>
{
// Since T implements IComparable, we can use CompareTo
if(v.CompareTo(lo)<0)
v=lo; // make sure v is not too low
if(v.CompareTo(hi)>0)
v=hi; // make sure v is not too high
return v;
}

Resources