In ceylon, how do I get Class object from a class? - ceylon

I have a method that takes a java.lang.Class object as a parameter. How do I get that from a Ceylon class?
That is, the equivalent of SomeClass.class in Java.

For SomeClass.class, use a meta literal: `SomeClass` for a closed model, `class SomeClass` for the open declaration.
For someInstance.class, you can use the type function from ceylon.language.meta.
import ceylon.language.meta { type }
class C() {}
class D() extends C() {}
shared void run() {
C c = D();
print(type(c));
}
Try it!
(type returns a closed model, i. e. with type arguments applied; you can get the open declaration with .declaration.)

Related

Delegated properties inside functions

I don't really understand why we can use delegated properties inside functions. We cannot create properties inside functions because inside functions we can only create variables.
How come is possible creating a delegated property inside a function then?
This line of code is a delegated property inside a function and I don't understand why is that possible.
val scoreFragmentArgs by navArgs<ScoreFragmentArgs>()
It has getters and setters and it doesn't make sense to me
Kotlin Delegates are based on storing the delegate object, and delegating getting/setting of the changes to it. So, it is possible to inline getValue calls when accessing to delegated variable.
For example:
import kotlin.properties.ReadOnlyProperty
import kotlin.reflect.KProperty
object Delegate : ReadOnlyProperty<Any?, Int> {
override fun getValue(thisRef: Any?, property: KProperty<*>): Int = 42
}
fun main() {
val foo by Delegate
println(foo)
}
The main method in Java will look like:
static final KProperty[] $$delegatedProperties = new KProperty[]{(KProperty)Reflection.property0(new PropertyReference0Impl(Reflection.getOrCreateKotlinPackage(MainKt.class, "123"), "foo", "<v#0>"))};
public static void main() {
System.out.println(Delegate.INSTANCE.getValue(null, $$delegatedProperties[0]));
}
As you see, accessing the variable is replaced by calling getValue.

Can I have a class in Typescript behave as a function?

In Typescript, we can have an interface that represents a function, which looks like so:
interface MyFunction {
(input1: A, input2: B, ...): ReturnType
}
Is it possible to do anything like this with a class? I'd like to have a class that behaves as a function, but I can't find any documentation on that.
I've tried the same format within a class, but that doesn't appear to work.
class Greeter {
private greeting: string = 'hello'
// this doesn't work
(name: string): string {
return `${this.greeting}, ${name}`
}
}
Can I have a class in Typescript behave as a function?
Not at the moment.
There is an issue tracking it : https://github.com/Microsoft/TypeScript/issues/183
There is also the call constructor proposal in ECMAScript : https://github.com/tc39/ecma262/blob/master/workingdocs/callconstructor.md

call one constructor from another constructors in one class

I've encountered the following question online.
If we call one constructor from another in a class, what will happen?
Can anyone give me some hints?
in java also its possible with the power of the this keyword. check out the example given below.
public class A {
public A() {
//this("a");
System.out.println("inside default Constructor");
}
public A(String a){
this();
System.out.println("inside Constructor A");
}
}
This concept is called constructor chaining. If it's c# i found this saying it's possible Is nesting constructors (or factory methods) good, or should each do all init work
This example from MSDN clarifies it
To add delegating constructors, constructor (. . .) : constructor (. . .) syntax is used.
class class_a {
public:
class_a() {}
// member initialization here, no delegate
class_a(string str) : m_string{ str } {}
// can’t do member initialization here
// error C3511: a call to a delegating constructor shall be the only member-initializer
class_a(string str, double dbl) : class_a(str) , m_double{ dbl } {}
// only member assignment
class_a(string str, double dbl) : class_a(str) { m_double = dbl; }
double m_double{ 1.0 };
string m_string;
};
Read answers from Can I call a constructor from another constructor (do constructor chaining) in C++? too.

hi.using super in extending

my code is like this
public class B {
public B(int f) {
}
}
public class A extends B{
int f=4;
public A() {
super(f);
}
}
why does it make a compile error?
In order to create an instance of class A, Java will behave as if it first creates an instance of B, meaning that the constructor of the super class, super(), will be called, (or in this case, you call it yourself) and then "adding on" the attributes of class A.
This is also why a super() call always has to be the first instruction in a constructor.
You are trying to pass an argument, which in a sense doesn't exist yet, because you try to read f before you called super().
What you could do is the following:
public class A extends B {
static final int F_CONST = 4;
int f = F_CONST;
public A() {
super(F_CONST);
}
}
Here F_CONST is a constant "static" variable, which is a "class variable", instead of an "object or instance variable". static members will be initialized when the class is loaded in memory, which is before any constructor can be called. The compiler is even allowed to replace F_CONST with just the value 4 directly, which would also be a simple solution.

Language Agnostic Basic Programming Question

This is very basic question from programming point of view but as I am in learning phase, I thought I would better ask this question rather than having a misunderstanding or narrow knowledge about the topic.
So do excuse me if somehow I mess it up.
Question:
Let's say I have class A,B,C and D now class A has some piece of code which I need to have in class B,C and D so I am extending class A in class B, class C, and class D
Now how can I access the function of class A in other classes, do I need to create an object of class A and than access the function of class A or as am extending A in other classes than I can internally call the function using this parameter.
If possible I would really appreciate if someone can explain this concept with code sample explaining how the logic flows.
Note
Example in Java, PHP and .Net would be appreciated.
Let's forget about C and D because they are the same as B. If class B extends class A, then objects of type B are also objects of type A. Whenever you create an object of type B you are also creating an object of type A. It should have access to all of the methods and data in A (except those marked as private, if your language supports access modifiers) and they can be referred to directly. If B overrides some functionality of A, then usually the language provides a facility to call the base class implementation (base.Foo() or some such).
Inheritance Example: C#
public class A
{
public void Foo() { }
public virtual void Baz() { }
}
public class B : A // B extends A
{
public void Bar()
{
this.Foo(); // Foo comes from A
}
public override void Baz() // a new Baz
{
base.Baz(); // A's Baz
this.Bar(); // more stuff
}
}
If, on the other hand, you have used composition instead of inheritance and B contains an instance of A as a class variable, then you would need to create an object of A and reference it's (public) functionality through that instance.
Composition Example: C#
public class B // use A from above
{
private A MyA { get; set; }
public B()
{
this.MyA = new A();
}
public void Bar()
{
this.MyA.Foo(); // call MyA's Foo()
}
}
depending on the access level (would be protected or public in .NET), you can use something like:
base.method(argumentlist);
the base keyword in my example is specific to C#
there is no need for an instance of class A, because you already have a class A inherited instance
Basically you need a reference to the parent class.
In PHP:
parent::example();
From: http://www.php.net/manual/en/keyword.parent.php
<?php
class A {
function example() {
echo "I am A::example() and provide basic functionality.<br />\n";
}
}
class B extends A {
function example() {
echo "I am B::example() and provide additional functionality.<br />\n";
parent::example();
}
}
$b = new B;
// This will call B::example(), which will in turn call A::example().
$b->example();
?>
I find that the best way to tame the complexity of inheritance is to ensure that I only make B inherit from A when it really is a specialization of the superclass. At that point, I can call A's methods from inside B just as if they were B's own methods, and if B has overridden them then I can only suppose that this must be for a good reason.
Of course, quite often it is useful for B's implementation of a method to invoke A's implementation on the same object, generally because the subclass is wrapping extra behavior around the superclass's basic definition. The way in which you do this varies between languages; for example, in Java you do this:
super.methodName(arg1, ...);
Here's a quick Java example:
public class Aclass
{
public static void list_examples()
{
return("A + B = C");
}
}
public class Bclass extends Aclass
{
public static void main(String [] args)
{
System.out.println("Example of inheritance "+list_examples);
}
}
Note that the method for accessing the parent class shouldn't change. Because you are extending you shouldn't have to say parent:: or anything unless you are overriding the parent method / function.
It seems to me that extending your class might not be your best option. Class "B", "C", and "D" should only extend class "A" if they are truly an extension of that class, not just to access some method. For instance "Huffy" should not extend "BrandNames" just because "Huffy" is a brand name and you want access to one of the methods of "BrandNames". "Huffy" should instead extend "Bicycle" and implement an interface so the methods of "BrandNames" can be used. An additional benefit here is that (in Java) multiple interfaces can be used but a class can only be extended once. If in your example class "B"' needed to access a method from class "A" that could work, but if class "C" needed to access a method from class "A" and class "'B"' then you would have to use an interface in class "'C".