Kotlin Cast String as String.Companion - string

So I'm new to learning Kotlin, coming from a C# background. I've been messing around with types and a few other things. I'm trying to create a static class of "WalMartGreeters" that can add greeters to a list, and then call a simple static function to display those. My method(s) takes a string argument/string list to add to the mutable string list but when I attempt to add values to it. I get a pre-compilation error saying "expected String.Companion" "got String" I attempt to change it to cast the String as a String.Companion and then it says the cast is illegal.
The predominant error I get is: Exception in thread "main" java.lang.ClassCastException: java.lang.String cannot be cast to kotlin.jvm.internal.StringCompanionObject
fun main(args:Array<String>) {
walMartGreeter.addTo("Jean")
walMartGreeter.addTo("Dale")
walMartGreeter.addTo("Phil")
walMartGreeter.addTo("Chris")
walMartGreeter.listTheWholeFam()
}
class walMartGreeter(val strName:String) {
companion object classList {
var prntList = mutableListOf(String)
fun addTo(strNameToAdd:String) {
prntList.add(strNameToAdd)
}
fun addTo(listOfNames:List<String>) {
for (item in listOfNames) {
prntList.add(item)
}
}
fun listTheWholeFam() {
//println("All I do is $strName")
for(item in prntList) {
println("Hello, $item!")
}
//println("type of mutList: ${mutList.ToString()}")
if(prntList is MutableList) {
println("Yeah, it's a mutableList");
}
}
}
}

First, the code above in your question can't be compiled since the prntList is a MutableList<String.Companion> rather than a MutableList<String>. IF you want the prntList to adds Strings, you need to change its type to MutableList<String>, for example:
var prntList = mutableListOf<String>()
Secondly, the String in mutableListOf(String) is just an qualifier rather than a class. which means the String will be reference to a specific reference in its scope. in your case the qualifier will reference to its companion object.
Members of the companion object can be called by using simply the class name as the qualifier.
For example:
// v--- reference to its companion object
val it:String.Companion = String
On the other hand, you also can make the qualifier reference to a top-level variable, for example:
val String = ""
// v--- reference to the top-level variable
var prntList = mutableListOf(String)
// ^---is a `MutableList<String>` rather than `MutableList<String.Companion>` now
The different between the mutableListOf<String>() and the mutableListOf(String) as below:
// v--- it is a type argument of the `String` class
mutableListOf<String>()
// v--- it is a qualifier that reference to `String.Companion`
mutableListOf(String)

Related

haxe get type info for fields of a function type parameter

Reflect can list an object's fields at runtime, but doesn't have type info. rtti has type info, but doesn't seem to work for type parameters.
In this example, I want MyLibrary to be able to figure out that T's val field is an Int.
import haxe.Constraints;
class Main {
static function main() {
var obj :IntStruct = MyLibrary.foo();
}
}
#:rtti
class IntStruct {
public var val :Int;
public function new() { }
}
class MyLibrary {
#:generic
public static function foo<T:Constructible<()->Void>>() :T {
var something = new T();
trace(Reflect.fields(something)); // [val]
trace(haxe.rtti.Rtti.getRtti(T)); // fails here
return something;
}
}
this gives this compile error:
Main.hx:20: characters 38-39 : Only #:const type parameters on #:generic classes can be used as value
if I add #:const to the type parameter declaration, I get this instead
Main.hx:20: characters 38-39 : foo.T should be Class<Unknown<0>>
Main.hx:20: characters 38-39 : ... For function argument 'c'
Since foo is generic, the compiler will write a separate function for each T, so it should be no problem for it to tell me about T inside foo. How can it be done?

groovy immutable object with parent class

I have two immutable groovy classes that have a few shared values that I'm trying to abstract to a parent class. However when I create the following, the second test case always fails. Although everything compiles correctly and no error is thrown at runtime, when I assign the parent property int he constructor, it is never set, resulting in a null value. I havent found any documentation that forbids this, but I'm wondering is this even possible? I've tried a number of configuration of Annotations and class-types (e.g. removing abstract from the parent) but nothing seems to work short of just removing the #Immutable tag altogether.
abstract class TestParent {
String parentProperty1
}
#ToString(includeNames = true)
#Immutable
class TestChild extends TestParent {
String childProperty1
String childProperty2
}
class TestCase {
#Test
void TestOne() {
TestChild testChild = new TestChild(
childProperty1: "childOne",
childProperty2: "childTwo",
parentProperty1: "parentOne"
)
assert testChild
assert testChild.parentProperty1
}
}
Based on the code for the ImmutableASTTransformation, the Map-arg constructor added by the createConstructorMapCommon method does not include a call to super(args) in the method body.
which means that immutable classes are self contained by default
Now if you want to do it you need to use composition instead of inheritance and this is an example of how you can do it :
import groovy.transform.*
#TupleConstructor
class A {
String a
}
#Immutable(knownImmutableClasses=[A])
class B {
#Delegate A base
String b
}
def b = new B(base: new A("a"), b: "b")
assert b.a
i hope this will help :)

In Haxe, how do you pass Enum values in functions, and then convert them to Strings within the function?

I can't seem to get this working, but I'd be surprised if it wasn't possible in Haxe.
I'm trying to pass a couple of Enum values defined in my game to a function, so that it can then concatenate them as String types and pass that to other functions.
Example:
// In a general Entity class:
public override function kill():Void {
messages.dispatchCombined(entityType, ListMessages.KILLED);
super.kill();
}
And in my Messages.hx class:
package common;
import msignal.Signal.Signal1;
/**
* A Message / Event class using Signals bound to String names.
* #author Pierre Chamberlain
*/
class Messages{
var _messages:MessagesDef;
public function new() {
_messages = new MessagesDef();
}
public function add(pType:String, pCallback:FuncDef) {
if (_messages[pType] == null) {
_messages[pType] = new Signal1<Dynamic>();
}
var signals = _messages[pType];
signals.add( pCallback );
}
public function dispatch(pType:String, pArg:Dynamic):Bool {
var signals = _messages[pType];
if (signals == null) return false;
signals.dispatch(pArg);
return true;
}
//Compiler doesn't like passing enums :(
public inline function addCombined(pSource:Enum, pEvent:Enum, pCallback:FuncDef) {
add( combine(pSource, pEvent), pCallback );
}
public inline function dispatchCombined(pSource:Enum, pEvent:Enum, pArg:Dynamic):Bool {
return dispatch( combine(pSource, pEvent), pArg);
}
//How can I just pass the enum "names" as strings?
static inline function combine(a:Enum, b:Enum):String {
return String(a) + ":" + String(b);
}
}
typedef MessagesDef = Map<String, Signal1<Dynamic>>;
typedef FuncDef = Dynamic->Void;
Note how addCombined, dispatchCombined and combine expect an "Enum" type, but in this case I'm not sure if Haxe actually expects the entire Enum "class" to be passed (ie: ListMessages instead of ListMessages.KILLED) or if a value should work. Anyways, compiler doesn't like it - so I'm assuming another special Type has to be used.
Is there another way to go about passing enums and resolving them to strings?
I think you need EnumValue as parameter type (if it is only for enum values), and use Std.String to convert to String values.
static inline function combine(a:EnumValue, b:EnumValue):String {
return Std.string(a) + ":" + Std.string(b);
}
Of course that can be written smaller using String interpolation:
static inline function combine(a:EnumValue, b:EnumValue):String {
return '$a:$b';
}
Of course that can be 'more dynamic' using type parameters:
static inline function combine<A, B>(a:A, b:B):String {
return '$a:$b';
}
There is totally no need to use Dynamic as suggested. If you use Dynamic, you basically turn off the type system.
live example:
http://try.haxe.org/#a8844
Use Dynamic instead of Enum or pass them as Strings right away since you can always convert to enum from String if you need it later.
Anyway pass the enum as enum:Dynamic and then call Std.string(enum);
EDIT: Using EnumValue is definitely better approach than Dynamic, I use Dynamic in these functions because I send more than just Enums there and I am not worried about type safety in that case.

Overloading a method which accepts `object` as default parameter type

I need to be able to call a method and pass in an object of an unknown type
but then have the correct overload called. I also need a default implementation that accepts
object as its parameter type. What I'm seeing is that the default overload is the only one that ever gets used.
Here's the gist of what I'm trying to do:
class Formatter
{
private object Value;
public Formatter(object val){
Value = val;
}
public override string ToString()
{
return Format(Value);
}
private string Format(object value)
{
return value.ToString();
}
private string Format(DateTime value)
{
return value.ToString("yyyyMMdd");
}
}
Ok, so far so good. Now I want to be able to do this:
public static class FancyStringBuilder()
{
public static string BuildTheString()
{
var stringFormatter = new Formatter("hello world");
var dateFormatter = new Formatter(DateTime.Now);
return String.Format("{0} {1}", stringFormatter, dateFormatter);
}
}
The result of FancyStringBuilder.BuildTheString() is "hello world 2012-12-21 00:00:00.000", when I expected "hello world 20121221"
The problem is that the overload that accepts a DateTime is not being called, instead defaulting to the overload which accepts an object. How can I call the proper method without resorting to a messy switch statement?
In Formatter.ToString(), the override Formatter.Format(object) is always called. This is because the overload resolution happens at compile-time, not run-time. At compile-time, the only thing known about Value is that it's an object.
If you really want to distinguish incoming types, you'll need to do so in Formatter's constructor. In this case, rather than hanging on to the object, you could just call ToString() immediately and only store the formatted result:
class Formatter
{
string formattedValue;
public Formatter(object value)
{
formattedValue = value.ToString();
}
public Formatter(DateTime value)
{
formattedValue = value.ToString("yyyyMMdd");
}
public string ToString()
{
return formattedValue;
}
}
Note that this does assume that your object isn't changing between the time you create the Formatter object and the time Formatter.ToString() is called, or at the very least that it's okay to take a snapshot of the string representation at the time the Formatter is created.
This also assumes that you know the incoming types at compile-time. If you want a truly run-time-only solution, you'll have to use the "is" operator or a typeof() comparison.
If your goal is just to provide custom ToString() formatting based on the incoming type, I'd probably do it using a list that maps from types to format strings:
static class Formatter
{
private static List<Tuple<Type, string>> Formats;
static Formatter()
{
Formats = new List<Tuple<Type, string>>();
// Add formats from most-specific to least-specific type.
// The format string from the first type found that matches
// the incoming object (see Format()) will be used.
AddMapping(typeof(DateTime), "yyyyMMdd");
// AddMapping(typeof(...), "...");
}
private static void AddMapping(Type type, string format)
{
Formats.Add(new Tuple<Type, string>(type, format));
}
public static string Format(object value)
{
foreach (var t in Formats)
{
// If we find a type that 'value' can be assigned to
// (either the same type, a base type, or an interface),
// consider it a match, and use the format string.
if (t.Item1.IsAssignableFrom(value.GetType()))
{
return string.Format(t.Item2, value);
}
}
// If we didn't find anything, use the default ToString()...
return value.ToString();
}
}
With that, calling code then looks like:
Console.WriteLine(
"{0} {1}",
Formatter.Format(DateTime.Now),
Formatter.Format("banana"));
I think this is because the class constructor takes an object as parameter, and then assign that object to variable Value which is also an object. There for calling Format(object) since Value is of type object
Try this
public override string ToString()
{
if(Value is DateTime)
return Format(Convert.ToDateTime(Value)); //this should call the right method
return Format(Value); //works for other non-custom-format types e.g. String
}

what does mean this part of paragraph? ( from c#4.0 Herbert schildt )

The use of ref and out is not limited to the passing of value types. They can also be used
when a reference is passed. When ref or out modifies a reference, it causes the reference,
itself, to be passed by reference. This allows a method to change what object the reference
refers to.
what does mean this part ?
When ref or out modifies a reference, it causes the reference,
itself, to be passed by reference. This allows a method to change what object the reference
refers to.
It means that by using ref you can change which object a variable points to, not only the contents of the object.
Let's say that you have a method with a ref parameter, that replaces an object:
public static void Change(ref StringBuilder str) {
str.Append("-end-");
str = new StringBuilder();
str.Append("-start-");
}
When you call it, it will change the variable that you call it with:
StringBuilder a = new StringBuilder();
StringBuilder b = a; // copy the reference
a.Append("begin");
// variables a and b point to the same object:
Console.WriteLine(a); // "begin"
Console.WriteLine(b); // "begin"
Change(b);
// now the variable b has changed
Console.WriteLine(a); // "begin-end-"
Console.WriteLine(b); // "-start-"
You can do something like this:
MyClass myObject = null;
InitializeIfRequired(ref myObject);
// myObject is initialized
...
private void InitializeIfRequired(ref MyClass referenceToInitialize)
{
if (referenceToInitialize == null)
{
referenceToInitialize = new MyClass();
}
}

Resources