How can I create abstract method to create BehaviorRelay in Swift? - abstract

I want to create an abstract method which can return an Observable or Relay.
It gives me below error i.e. use of undeclared type 'type'

Related

Why static variable can not be accessed by this keyword in static method if we pass static method in any route's controller(NODEJS)?

We can access static variables in static method by using 'this' keyword but this statement does not work if we pass that static method in any route. Keyword 'this' has refrence to class but its value is undefined. How?
Actucal code is big, I have given only problematic code if you want you can run it.
Router code :-
import UserController from './user-controller';
router.post('/register', UserController.register);
/////////////////////////////////////////////////////////
Controller Code :-
static someString = 'myString';
static register() {
console.log(this.someString);
}
}
export default UserController;
/////////////////////////////////////////////////////////
Error :-
TypeError: Cannot read properties of undefined (reading 'someString')
at register (/home/user/Desktop/TS-Pro/src/modules/user/user-controller.ts:5:22)
at Layer.handle [as handle_request] (/home/user/Desktop/TS-Pro/node_modules/express/lib/router/layer.js:95:5)
at next (/home/user/Desktop/TS-Pro/node_modules/express/lib/router/route.js:144:13)
at Function.joiValidation (/home/user/Desktop/TS-Pro/src/utils/validator.ts:41:7)
at /home/user/Desktop/TS-Pro/src/modules/user/user-validator.ts:11:22
at Generator.next (<anonymous>)
at /home/user/Desktop/TS-Pro/src/modules/user/user-validator.ts:8:71
at new Promise (<anonymous>)
at __awaiter (/home/user/Desktop/TS-Pro/src/modules/user/user-validator.ts:4:12)
at register (/home/user/Desktop/TS-Pro/src/modules/user/user-validator.ts:18:16)
Some general Object Oriented stuff:
A Class is a blueprint. Example: Vehicle
An Object is a specific item created using that blueprint. Example: car (of type Vehicle)
Keywords like this/me/self are used to refer to an instance of an Object from within the same object. I want to emphasize, that an instance of an object means a specific car object and not the Vehicle class.
Keyword static marks an attribute/method as being Class-level (i.e. you can call it without creating an object). e.g. Vehicle.Accelerate and not car.accelerate.
Keyword static ALSO makes that attribute global. i.e. you can change it in 1 object and it will change for all objects, because it's actually sitting in central blueprint, and not a specific object. (not relevant to your question, but it's the next mistake people make)
The short version: static methods only work with static methods/variables. Instance methods (i.e. methods without static) can access all methods/variables. (Ignoring public/private visibility)
In most high level languages this is inaccessible in static context and your code would cause a syntax error, but JavaScript doesn't many of the limitations because any text can be almost anything (method, class, variable and more) and turn into almost anyting mid-way. It's something you have to keep in mind.
You get an error, because JS compiler doesn't check for undefined variables ahead of time and you are trying to access a different (non-static) variable by using keyword this.
Finally, To access that variable either use that variable name directly (while within the class), or use ClassName.variableName syntax.

Generic type vs dynamic vs object

I want to make the return type of my method generic. The caller will decide which type it should expect.
Actually my method will be a member of interface and the class which will implement it will have a decision making block to delegate the work to other methods.
Hence I want to make the return type of the interface method as generic.
I can achieve this by using dynamic or object keyword or c# generic type.
I am not able to figure it out which will be the best option to achieve it and what are the limitations and advantages of each type.
public interface ICoreWrapper
{
Response<T> ExecuteDeviceCommand<T>(DeviceCommand deviceCommand, object param = null);
}
Please suggest me.
Thanks in advance.
If you do not know the type at compile time you could use dynamics but they will be slower because they are using runtime invocation and less safe because if the type doesn't implement the method you are attempting to invoke you will get a runtime error.
Use dynamic return type, Based on the input type return the appropriate object.

Cannonical way to do circular dependency in Nim

Suppose we have two modules: one defines an Object and one defines an ObjectFactory. The Object needs to have access to the ObjectFactory use some of its functions, and the ObjectFactory needs access the Object to be able to instantiate Objects.
What is the cannonical way to solve this in Nim if the Object is implemented in a module and the ObjectFactory is implemented in another module?
I am assuming that the issue here is with mutually recursive types, i.e. where the declarations of two or more types refer to one another. Methods or procedures that refer to one another are handled fine by mutually recursive imports, though one has to be careful with module initialization in this case.
As in most other languages that normally require mutually recursive types to be within the same module/compilation unit, there are two principal answers.
One solution is to have the two types within the same module that is imported by both the module that declares the object type and the module that declares the factory type (both types still need to be part of the same type clause). E.g., you create a separate file, called something like factory_types.nim, and put both types in it:
type
ObjectFactory = ref object
lastValue: Object
x: proc(): Object
Object = ref object
factory: ObjectFactory
This module would then be imported by both the module implementing the object and the module implementing the factory.
The other solution, where you can keep each type in its module, is parametric polymorphism, where a type parameter is used as a forward declaration. E.g., you do:
type
ObjectFactory[TargetType] = ref object
lastValue: TargetType
generator: proc(): TargetType
and elsewhere:
type
Object = ref object
factory: ObjectFactory[Object]

Interfacing and extending ApplicationClass

I am trying to write a module in F#, making it easier working with Excel, by extracting rows, columns, etc. along with type casting and so on. One of the first things I wanted to do, was to extend various classes/types to implement the IDisposable interface. I tried to write something like the following
type Excel.ApplicationClass with
interface IDisposable with
member this.Dispose() =
this.excel.Quit()
Marshal.ReleaseComObject(this.excel) |> ignore
What I wasn't aware of, was that I would get the following error "All implemented interfaces should be declared on the initial declaration of the type".
My question is the following: Since I am not allow to extend a type with an interface - what else could I do?
If you inherit from the base class it can work, like this
type myAppClass() =
inherit Excel.ApplicationClass() //may not be correct signature - you need to match the base constructor
interface IDisposable with
member this.Dispose() =
//function body

What is <T> in a class definition?

What exactly are we telling the compiler in this line of code ?
public abstract class RepositoryBase<T> where T:class
Are we saying that when we create an object that inherits from RepositoryBase, the object must take a class in the constructor otherwise there will be a compile error ?
It's saying that when you inherit from RepositoryBase<T>, the type T which you specify must be some type of class (or interface or etc., but not a value type). For example RepositoryBase<int> is illegal and won't compile: see Constraints on Type Parameters (C# Programming Guide).

Resources