Where I can find Kotlin String plus method implementation? - string

Where I can find Kotlin String plus method implementation?
When I track who calls String#plus, why Intellij IDEA point to "+" ? If String#plus is equal to "+", how does this implement?

I finally find the answer.
Where I can find Kotlin String plus method implementation?
Here I quote answer of ilya.gorbunov who is the member of JetBrains Team member.
"Currently, navigation to builtin sources may not work as expected in maven/gradle projects.
You can find sources of builtins here kotlin/core/builtins at master·JetBrains/kotlin· GitHub 171.
Note that the ‘native’ part of them do not have implementation bodies, as they are implemented as compiler intrinsics.
If you’re more interested in what they are compiled to, you can inspect the generated bytecode with the “Show Kotlin bytecode” action."
Link is here Browsing source code of kotlin.kotlin_builtins like the .. operator
When I track who calls String#plus, why Intellij IDEA point to "+" ?
If String#plus is equal to "+", how does this implement?
The "+" operator is implemented by "Operator overloading" of Kotlin. When view String.kt source code, you can see the below code.
package kotlin
public class String : Comparable<String>, CharSequence {
companion object {}
/**
* Returns a string obtained by concatenating this string with the string representation of the given [other] object.
*/
#kotlin.internal.IntrinsicConstEvaluation
public operator fun plus(other: Any?): String
// omitting some source code
}
Parameter of String#plus method is Any?, meaning we can use "+" operator concatenate any object after string object. Details of "Operator overloading" you can refer to Operator overloading Kotlin's Doc

Related

How to describe an operator method in UML

I have the following method:
operator fun get(key: String): String {
//awesome code
}
I have searched a lot, but I could not find anything which actually helped me understand how to describe the operator in the UML.
i am doing it like that:
+ get(in key: String) : String
I think the in is the right operator in this case right?
The best link I have found so far...:
www.uml-diagrams.org
UML does not have a standard way to declare operations to be operators, but you could define your own stereotype for it:
And then use it like this:
The keyword in is optional. If you don't specify in, out or inout, then the parameter is an input parameter by default. If your operator modifies key (I don't know if Kotlin supports that), then you should use keyword inout.

SOAPUI context variables - How does Groovy make this possible?

Sorry to all you Groovy dudes if this is a bit of a noob question.
In SOAPUI, i can create a Groovy script where i can define an arbitrary variable to the run context to retrieve at a later time.
context.previouslyUndefinedVariable = 3
def num = context.previouslyUndefinedVariable
What feature of Groovy allows previously undefined variables to be added to an object like this? I would like to learn more about it.
Many thanks in advance!
Groovy has the ability to dynamically add methods to a class through metaprogramming.
To learn more, see:
What is Groovy's MetaClass used for?
Groovy Goodness: Add Methods Dynamically to Classes with ExpandoMetaClass
Runtime and compile-time metaprogramming
The accepted answer is a bit of a poor explanation for how SoapUI is doing it.
In this case, context is always an instance of some SoapUI library java class (such as WsdlTestRunContext), and these are all implementations of Map. You can check context.getClass() and assert context in Map.
When you look up a property on a Map, Groovy uses the getAt and putAt methods. There are various syntaxes you can use. All of these are equivalent:
context.someUndef
context.'someUndef'
context[someUndef]
context['someUndef']
context.getAt('someUndef')
And
context.someUndef = 3
context.'someUndef' = 3
context[someUndef] = 3
context['someUndef'] = 3
context.putAt('someUndef', 3)
I like to use any of the above that include quote marks, so that Groovy-Eclipse doesn't flag it as a missing property.
It's also interesting that Groovy looks for a getAt() method before it checks for a get method being referred to as a property.
For example, consider evaluating "foo".class. The String instance doesn't have a property called class and it also doesn't have a method getAt(String), so the next thing it tries is to look for a "get" method with that name, i.e. getClass(), which it finds, and we get our result: String.
But with a map, ['class':'bar'].class refers to the method call getAt('class') first, which will be 'bar'. If we want to know what type of Map it is, we have to be more specific and write in full: ['class':'bar'].getClass() which will be LinkedHashMap.
We still have to specify getClass() even if that Map doesn't have a matching key, because ['foo':'bar'].class will still mean ['foo':'bar'].getAt('class'), which will be null.

Generate method or class comments in kotlin

I'm using Android Studio 3.0.1. I can hit fix doc comment to insert comment of method quickly in Java, but not effective in Kotlin. How can I solve it?
When you're typing # inside a kdoc, you'll see auto complete for #param, and after #param you'll see all parameters, and currently you cannot complete all parameters within one click.
Mention the /** */ comments are called kdoc in Kotlin, which is not javadoc.
Kotlin and especially KDoc encourage a different documentation style. As stated in this discussion:
The reason is that we found that referring to parameter names from the documentation text allows to write documentation that is more concise and easier to read compared to the traditional javadoc style where every parameter is documented in a separate tag. Therefore, we do not generate the template with parameter names by default. (D. Jemerov, Kotlin in Action Author)
Here’s an example of let, which is part of the standard library:
/**
* Calls the specified function [block] with `this` value as its argument and returns its result.
*/
#kotlin.internal.InlineOnly
public inline fun <T, R> T.let(block: (T) -> R): R

In Groovy, can I override java-style casting syntax on POJO classes?

I would like to be able to use plain java-style implicit/explicit casting instead of asType overrides so that sources written in Java work properly. I've overridden asType on String similarly to the approach suggested in How to overload some Groovy Type conversion for avoiding try/catch of NumberFormatException? like:
oldAsType = String.metaClass.getMetaMethod("asType", [Class] as Class[])
String.metaClass.asType = {Class typ ->
if (Foo.class.isAssignableFrom(typ)) {
Foo.myCast(delegate)
} else {
oldAsType.invoke(delegate,typ)
}
}
I'd like all of these options to work:
// groovy
String barString
Foo foo = barString asType(Foo.class) // asType works but
Foo foo = barString // implicit cast fails
Foo foo = (Foo) barString // explicit cast fails
The latter two fail because groovy is using DefaultTypeTransformation.castToType, which doesn't attempt to invoke new Foo() unless the object to be cast is either one of a slew of special cases or is some sort of Collection type.
Note that the solution Can I override cast operator in Groovy? doesn't solve the issue because the code that is doing the casting is regular Java code that I cannot alter, at least not at the source code level. I'm hoping that there is either a secret hook into casting or a way to override the static castToType method (in a Java class, called by another Java class - which Can you use Groovy meta programming to override a private method on a Java class says is unsupported)... or some other clever approach I haven't thought of.
Edit: The question is about using Java-style casting syntax, essentially to use groovy facilities to add an autoboxing method. Groovy calls this mechanism "casting," for better or worse (see DefaultTypeTransformation.castToType as referenced above). In particular, I have replaced an enum with a resourced class and want to retain JSON serialization. Groovy's JSON package automatically un/marshals enum values of instance members to strings and I'm trying to make the replacement class serialize compatibly with a minimal changes to the source code.
Part of the problem here is you are confusing conversion with casting. Using the "as" operator is not the same thing as imposing a cast. They seem similar, but they serve separate purposes.
Foo foo = (Foo) barString
That doesn't say something like "create a Foo out of barString". That says "Declare a reference named foo, associate the static type Foo with that reference and then point that reference at the object on the heap that the reference barString currently points to.". Unlike languages like C++, Groovy and Java do not allow you to ever get in a situation where a reference points at an object that is of a type that is incompatible with the reference's type. If you ever got into a situation where a Foo reference was pointing to a String on the heap, that would represent a bug in the JVM. It cannot be done. You can come up with ways to create Foo objects out of String objects, but that isn't what the code above is about.
The answer appears to be "no". Absent a rewrite of the DefaultTypeTransformation.castToType to allow for this sort of metaprogramming, the implication is to use another implementation strategy or use a different language.

How to add hashmap into ArrayList in c++?

I want to add my hashmap into ArrayList in vc++ 08. My code is below.
typedef std::tr1::unordered_map< std::wstring, std::wstring > hashmap;
hashmap numbers;
ArrayList^ myAL = gcnew ArrayList;
myAL->Add(numbers); // gives error...
But it gives error that
error C2664: 'System::Collections::ArrayList::Add' : cannot convert parameter 1 from 'hashmap' to 'System::Object ^'
1> No user-defined-conversion operator available, or
1> No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
I tried for cast with object, but not succeeded. Can anyone help me to add hashmap in arraylist?
Thanks in advance...
The operation you're trying above doesn't work because managed and native types don't interoperate directly in that way.
I suggest Kenny Kerr's classic C++/CLI article Best Practices for Writing Efficient and Reliable Code with C++/CLI to figure out your specific interop scenario in more detail, but offhand I think what you want to do is embed a pointer to your native object in a managed object which you'll be able to add to your list structure. If you use Mr. Kerr's AutoPtr class (described in the article above and updated here), you should be able to create a managed class containing the AutoPtr as a member, which you can add to your ArrayList.

Resources