Is a Swift Protocol equal to "extend" in typescript? - swift

I'm learning Swift at the moment and came across "protocols". They seem to me like a prebuild class that you extend, like in typescript, is that true?
Thanks, Pascal

I would compare them to interface in typescript.
interface FooBar {
foo: () => void
bar: () => void
}
class MyFooBar implements FooBar {
public foo(): void {
...
}
public bar(): void {
...
}
}

Related

How can I call super inside constructor body?

class Foo {
Foo(int y);
}
class Bar extends Foo {
int value;
Bar(int x) { // error in this line
value = x;
print("Hi there");
super(x); // error in this line
}
}
How can I call super inside constructor body?
Note:
I know I can use initialiser list to solve it but I would like to know how to call super inside method body?
Bar(int x): value = x, super(x); // works but I am not looking for it.
Dart does not support inheriting the constructors as explicitly callable methods. The initializer list you mentioned is the supported way to call unnamed super constructors in Dart.
However, you can achieve what you want with the help of named constructors. Take a look at the below example -
class Foo {
int superValue;
Foo(); //A default zero-argument constructor
Foo._init(this.superValue); //Named constructor
void initValue(int x) => Foo._init(x);
}
class Bar extends Foo {
int value;
Bar(int x) {
value = x;
print("Hi there");
super.initValue(x);
}
}
void main() {
Foo foo = Bar(10); //prints 'Hi there'
}
Hope it helps!
UPDATE
You can also call the super constructor and add other statements to the child constructor using this way -
class Foo {
int superValue;
Foo(this.superValue);
}
class Bar extends Foo {
int value;
Bar(int x) : super(x) {
value = x;
print("Hi there");
}
}
void main() {
Foo foo = Bar(10);
}

Is it possible to satisfy an interface member implementation using another interface in Kotlin?

For example, if I have an interface like this:
interface MyInterface {
fun thouMustImplementThis()
}
And I have a class MyClass that implement MyInterface which means I have to create an override for the function:
class MyClass : View, MyInterface {
override fun thouMustImplementThis() {
println ("Hello world")
}
}
Is it possible if I have another interface that implement the function:
interface YourInterface {
fun thouMustImplementThis() {
println ("Hello Stack Overflow")
}
}
So I can leave the implementation out from my class:
class MyClass : View, MyInterface, YourInterface {
}
But I found out that I still have to implement the function, albeit I only need to add a call to its super function version.
class MyClass : View, MyInterface, YourInterface {
override fun thouMustImplementThis() {
super.thouMustImplementThis()
}
}
and I don't want this.
The point is, I want to create some kind of default implementation for some native interfaces so that I don't have to reimplement them each time I create a class based on those interfaces. I was thinking that by making it as an interface, I can "attach" the implementation as I need. Is there any workaround for this?
You can just have the interface implement the other interface.
Like this:
interface YourInterface : MyInterface {
override fun thouMustImplementThis() {
println("Hello Stack Overflow")
}
}
Now, the class can be implemented like this (no need for a body):
class MyClass : View, YourInterface

How to get the parameter type of the class method with macro?

How to get the parameter type of the class method with macro?
class A{
public function new(){
//this how get method out arg[0] Type with macro?
var arg0IsInt:Bool=arg0IsInt(out);
}
public function out(a:Int){ return true; }
macro public function arg0IsInt(e:Expr):Bool{
}
}
I'm going to call a method that has a parameter for a type when I construct a letter.
You can pass out to the expression macro and then use Context.typeof() on it. The result will be a function type (TFun) whose first argument you can inspect using pattern matching.
Here's a working example:
import haxe.macro.Context;
import haxe.macro.Expr;
class Main {
static function main() {
new Main();
}
public function new() {
trace(arg0IsInt(out)); // true
trace(arg0IsInt(out2)); // false
}
public function out(a:Int) {}
public function out2(a:Float) {}
macro static function arg0IsInt(func:Expr):Expr {
return switch Context.typeof(func) {
case TFun(args, _):
switch args[0].t {
case TAbstract(_.get() => t, _) if (t.name == "Int" && t.pack.length == 0):
macro true;
case _:
macro false;
}
case _:
throw 'argument should be a function';
}
}
}
Int is a an abstract, and to make sure it's not just some random abstract that happens to be named Int in some other package, we check that it's in the toplevel package (pack.length == 0).
In fact you can go very far with pattern matching:
import haxe.macro.Context;
import haxe.macro.Expr;
class Test {
static function main() {
new Test();
}
public function new() {
trace(arg0IsInt(out)); // true
trace(arg0IsInt(out2)); // false
}
public function out(a:Int) {}
public function out2(a:Float) {}
macro static function arg0IsInt(func:Expr):Expr {
return switch Context.typeof(func) {
case TFun(_[0] => {t: TAbstract(_.get() => {name: 'Int', pack: []}, _)}, _): macro true;
case TFun(_): macro false;
case _: throw 'argument should be a function';
}
}
}

What is the proper way to code Kotlin's by-clause (a.k.a. Class Delegation) in Swift?

I need to re-code in Swift a Kotlin class defined like this:
class A(private val foo: FooInterface = FooBase()) : FooInterface by foo {
...
}
Is the only way to achieve that is by directly extending the class A with the FooInterface protocol and redirecting all calls to a local private Foo instance?
extension A: FooInterface {
func fooFun1() {
self.privateFooInstance.fooFun1()
}
}
What is the most concise way to do that?
As you already know, Swift does not have direct support for Class Delegation.
So, you may need more code than Kotlin, which has direct support for Delegation. But, instead of extending each class which implements the protocol, you can add default implementation for delegation.
protocol FooInterface {
func fooFun1()
//...
}
protocol FooDelegateable {
var fooDelegate: FooInterface {get}
}
extension FooInterface where Self: FooDelegateable {
func fooFun1() {
self.fooDelegate.fooFun1()
}
//...
}
struct SomeFoo: FooInterface {
func fooFun1() {
print("FooInterface is delegated to SomeFoo.")
}
}
class A: FooInterface, FooDelegateable {
private let foo: FooInterface
//FooDelegateable
var fooDelegate: FooInterface {return foo}
init(_ foo: FooInterface) {
self.foo = foo
}
//...
}
let a = A(SomeFoo())
a.fooFun1() //->FooInterface is delegated to SomeFoo.
How is it?

Overriding abstract method using anonymous class

I'm trying to override a abstract method using anonymous class but it shows error like Class A is not abstract and abstract method eat() not implemented.Please Help me out
package javaapplication1;
abstract class D
{
abstract void eat();
}
public class A extends D
{
public static void main(String[] args)
{
D obj_d=new D(){
#Override
void eat()
{
System.out.println("I'm eating!");
}
};
obj_d.eat();
}
}