Map's keys method keys ():Iterator<K> gets converted to public java.lang.Object keys() in Java. Is there a way to generate an actual Iterator type?
Thanks!
Not without changing the generator code, because Iterator<T> is an anonymous structure type, which is represented by Object at run-time. Looking at the actual implementation, it's also implemented as an anonymous object, so Object is the best one can have at run-time. However, it could be reworked to some concrete class which would be returned by StringMap/IntMap/ObjectMap iterator method made inline.
Related
Assuming that I want that following Value Object contains always capitalized String value. Is it eligible to do it like this with toUpperCase() in constructor?
class CapitalizedId(value: String) {
val value: String = value.toUpperCase()
// getters
// equals and hashCode
}
In general, I do not see a problem of performing such a simple transformation in a value object's constructor. There should of course be no surprises for the user of a constructor but as the name CapitalizedId already tells you that whatever will be created will be capitalized there is no surprise, from my point of view. I also perform validity checks in constructors to ensure business invariants are adhered.
If you are worried to not perform operations in a constructor or if the operations and validations become too complex you can always provide factory methods instead (or in Kotlin using companion, I guess, not a Kotlin expert) containing all the heavy lifting (think of LocalDateTime.of()) and validation logic and use it somehow like this:
CapitalizedId.of("abc5464g");
Note: when implementing a factory method the constructor should be made private in such cases
Is it eligible to do it like this with toUpperCase() in constructor?
Yes, in the sense that what you end up with is still an expression of the ValueObject pattern.
It's not consistent with the idea that initializers should initialize, and not also include other responsibilities. See Misko Hevery 2008.
Will this specific implementation be an expensive mistake? Probably not
Is there any practical difference between the following two approaches to casting:
result.count = (int) response['hits']['total']
vs
result.count = response['hits']['total'] as int
I'm using #CompileStatic and the compiler is wanting me to do the cast - which got me wondering if there was any performance or practical difference between the two notations.
The main difference is casting uses the concept of inheritance to do the conversion where the as operator is a custom converter that might or might not use the concepts of inheritance.
Which one is faster?
It depends on the converter method implementation.
Casting
Well, all casting really means is taking an Object of one particular
type and “turning it into” another Object type. This process is called
casting a variable.
E.g:
Object object = new Car();
Car car = (Car)object;
As we can see on the example we are casting an object of class Object into a Car because we know that the object is instance of Car deep down.
But we cant do the following unless Car is subclass of Bicycle which in fact does not make any sense (you will get ClassCastException in this case):
Object object = new Car();
Bicycle bicycle = (Bicycle)object;
as Operator
In Groovy we can override the method asType() to convert an object
into another type. We can use the method asType() in our code to
invoke the conversion, but we can even make it shorter and use as.
In groovy to use the as operator the left hand operand must implement this method:
Object asType(Class clazz) {
//code here
}
As you can see the method accepts an instance of Class and implements a custom converter so basically you can convert Object to Car or Car to Bicycle if you want it all depends on your implementation.
If I remember correctly even when we pass large string or byte array as a parameter to a method, it only passes the pointer to the heap of the data rather than the full data. So it should not degrade any performance or it should not pile up memory unnecessarily. Just want to confirm if my understanding is correct regarding the above statement?
I know it is better to keep the string or byte array as a private variable in the class and access it in every method it required thus eliminating one additional parameter from the method call.
Thanks
If I remember correctly even when we pass large string or byte array as a parameter to a method, it only passes the pointer to the heap of the data rather than the full data.
Yes, when you pass any reference type argument, just the reference is passed, by value. Note that this is not the same as "pass by reference", which is applicable to parameters of both reference types and value types.
See my articles on reference types and value types and parameter passing for more information.
I know it is better to keep the string or byte array as a private variable in the class and access it in every method it required thus eliminating one additional parameter from the method call.
That entirely depends on the context. Is it logically part of the state of the class or not?
I am currently reading an existing code in MS Visual C++ 6.0. I notice a code pattern where they cast object into a structure.
There is a CMemory object.
CMemory a;
MY_STRUCTURE_A* a = (MY_STRUCTURE_A*)(void *)a;
MY_STRUCTURE_B* a = (MY_STRUCTURE_B*)(void *)a;
I checked the Custom memory class and it really is a class object. It does have a = operator defined but I do not think that would allow it to be reinterpreted to a structure. Why is this being done. How is an object type being cast to different objects?
Any idea why this is being done? I know there is a reinterpret_cast and I am guessing that this technique of casting to void pointer to a structure pointer is similar. But I am not sure if it is the same. Is this pattern safe casting a class object to a struct?
Note: the CMemory is just an arbritary name of the object used. It is not part of the MFC class.
Added based on Necrolis' comment.
The CMemory and it has only 3 members declared in the following order (1) char pointer, (2) int specifying the allocated memory of (1), and (3) a previous and next pointer to other instance of CMemory. It also has a lot of member method. From what I understand, even if I directly cast a class to a structure. The class would start should start with the first member variable which is the char pointer.
class CMemory {
public:
CAMemory();
... Other methods
private:
char *m_pMemory;
int m_memorySize;
... Other field
}
Going by the name of the class and the casting, CMemory is more than likely a generic memory block tag (for a GC, arbitrary hash table etc), and to access the memory its tagging requires a cast. Of course this is a "best guess", it means nothing without seeing the full code for CMemory.
Is this safe, totally not, its not only UB, but there is no check (at least in your example) as to whether the object you casting to is the object represented by the memory layout. Also, with this being C++, they should be avoiding C casts (as you have noted. the double cast is in fact to get around compiler errors/warnings, which is always the worst way to solve them).
Can any one help me in finding the basic difference between mutable and immutable?
Immutable means that once initialized, the state of an object cannot change.
Mutable means it can.
For example - strings in .NET are immutable. Whenever you do an operation on a string (trims, upper casing, etc...) a new string gets created.
In practice, if you want to create an immutable type, you only allow getters on it and do not allow any state changes (so any private field cannot change once the constructor finished running).
A very basic definition would be:
Mutable Objects: When you have a reference to an instance of an object, the contents of that instance can be altered
Immutable Objects: When you have a reference to an instance of an object, the contents of that instance cannot be altered
Immutable means "cannot be modified after it is created".
An immutable type has a constructor and getters but not setters.
A mutable type can also have setters.
An example of an immutable type is DateTime. The method AddMinutes does not modify the object - it creates and returns a new DateTime.
Another example is string. For a mutable class similar to string you can use the class StringBuilder.
There is no keyword in C# to declare a type as immutable. Instead you should mark all member fields as readonly to ensure that they can only be set in the constructor. This will prevent you from accidentally modifying one of the fields, breaking the immutability.
An immutable type cannot be changed once instantiated. For example strings are immutable. Every time you want to change the value of a string a new instance is created.
Immutable variables using in functional languages.
Using a term variable is inappropriate and functional programmers
prefer the term value.
Advantages of Immutability:
1 Thread Safety
2 Sharing
3 Less error prone
So prefer immutability, if you have the choice. :)
Use Reference
imaginationhunt.blogspot
IS STRING MUTABLE OR IMMUTABLE IN .NET?
Mutable: Mutable means whose state can be changed after it is created.
Immutable: Immutable means whose state cannot be changed once it is created.
String objects are 'immutable' it means we cannot modify the characters contained in string also operation on string produce a modified version rather than modifying characters of string.