override setter with call to inherited setter - dart

I am a little bit confused: can I override a setter / getter but still use the super setter/getter? If yes - how?
Use case:
class A {
void set value(num a) {
// do something smart here
}
}
class B extends A {
void set value(num a) {
// call parent setter and then do something even smarter
}
}
If this is not possible how can one still preserve the API but expand the logic in the new class. The users of the code already use instance.value = ... so I do not want to change it to method call is possible.
Please help:)

You can access to parent with super. :
class B extends A {
void set value(num a) {
super.value = a;
}
}

Only need call super.value = a
class A {
void set value(String value) {
print(value.toUpperCase());
}
}
class B extends A {
void set value(String value) {
super.value = value;
print(value.toLowerCase());
}
}
void main() {
B b = new B();
b.value = "Hello World";
}

Related

Calling Generic Types function

is it possible to call generic types function.
if not is there a different approach to something like this.
someFunction<T>(){
T.anotherFunction();
}
EDIT
MyModel model = NetworkClient.sendRequest<MyModel>(url);
static Future<T> sendRequest<T>(String URL){
//send request
var res = data.toString();
return T.fromJson(json.decode(res))
}
void main() {
someFunction(Foo());
someFunction(Bar());
}
someFunction<T>(T t) {
if (t is Foo)
t.fooFunc();
else if (t is Bar)
t.barFunc();
else
throw Exception("Unknown type: ${t.runtimeType}");
}
class Foo {
void fooFunc() {
print("foo");
}
}
class Bar {
void barFunc() {
print("bar");
}
}
Put your method (lets say MyMethod) in an interface or base class. Lets say MyInterface.
Then use a constraint on your generic type:
someFunction<T extends MyInterface>(T t){
t.MyMethod();
}
Since you cannot have constructors in an interface, your plan with the fromJson might not work out. I don't know your class structure. But you can write generics with specific constraints in mind.

Call edit method from a main class in X++

I want to recall a value of an edit method declared inside a form from a class main. How can I do it?
[Form]
public class AdvancedCustomerSchedule extends FormRun
{
Sorting sorting;
edit Sorting edtSorting(boolean set, Sorting _sorting)
{
if (set)
{
sorting = _sorting;
}
return sorting;
}
}
and the class:
class AdvancedCustomerScheduleService
{
static void main(Args args)
{
//I want to call the method edtSorting here.
}
}
UPDATE
FormRun callerForm;
if (args.caller() is FormRun)
{
callerForm = args.caller() as FormRun;
if (formHasMethod(callerForm, identifierStr(edtSorting)))
{
str test = callerForm.edtSorting();
info(test);
}
}
For calling methods defined on forms in general you usually use a pattern like the following:
...
FormRun callerForm;
...
if (_args.caller() is FormRun)
{
callerForm = _args.caller();
if (formHasMethod(callerForm, identifierStr(someMethod)))
{
callerForm.someMethod();
}
...
Have a look at class DirPartyContactInfoFormHandler and its static main method for an example.

In Dart, how can I call a superclass method shadowed by a mixin method?

I have a class A and it extends B and mixes in C like the code below. How do I make it work?
class A extends B with C {
#override
int getValue() {
// Call's C's getValue.
super.getValue();
// Now I want to call B's getValue. How do I do that?
return B.getValue(); // Doesn't work.
}
}
class B {
int getValue() {
return 1;
}
}
class C {
int getValue() {
return 2;
}
}
How can I execute B's getValue? Thanks.
The mixin method from C shadows the method from B, so you can't use super directly to refer to the B method. You need to introduce a way to access it.
What you can do is to put a private redirecting function in between the B class and the C mixin application:
class A extends B with _Helper, C {
int getValue() {
// It's C
super.getValue();
// Now I want to get B's getValue how do it?
return super._getValueFromB();
}
}
class B {
int getValue() {
return 1;
}
}
mixin C {
int getValue() {
return 2;
}
}
mixin _Helper on B {
int _getValueFromB() => super.getValue();
}
Since the mixin application order is B, B-with-_Helper, B-with-_Helper-with-C, the super of the B-with-_Helper superclass is B, and the _getValueFromB will access it correctly.

Can I call a class method without calling the overridden method

I want to call a sibling method without calling the overridden method. Seems like casting this to the base class type should work, but doesn't.
Specifically, I want to call Base.bar() from Base.foo() without calling any overrides of that method (from Subclass).
class Base {
void foo() {
print("Base.foo");
(this as Base).bar(); // I *don't* want to invoke Subclass.bar
}
void bar() {
print("Base.bar");
}
}
class Subclass extends Base {
#override
void bar() {
print("Subclass.bar");
super.bar();
}
}
x = Subclass();
x.foo();
// Expected Output
// Base.foo
// Base.bar
// Actual Output
// Base.foo
// Subclass.bar
// Base.bar
This is a kludge until a good answer is given.
class Base {
void foo() {
print("Base.foo");
// can't do this => (this as Base).bar();
Base_bar();
}
void Base_bar() {
print("Base.bar");
}
void bar() {
Base_bar();
}
}
class Subclass extends Base {
#override
void bar() {
print("Subclass.bar");
super.bar();
}
}
Try this
class Subclass extends Base {
#override
void bar() {
super.bar();
}
}

Override method in dart on fly (like JAVA)

Is there way to overriding method in Dart like JAVA, for example:
public class A {
public void handleLoad() {
}
}
And when overriding:
A a = new A() {
#Override
public void handleLoad() {
// do some code
}
};
No, Dart does not have anonymous classes. You have to create a class that extends A and instantiate it.
No but it much less useful in Dart because you can just reassign function:
typedef void PrintMsg(msg);
class Printer {
PrintMsg foo = (m) => print(m);
}
main() {
Printer p = new Printer()
..foo('Hello') // Hello
..foo = ((String msg) => print(msg.toUpperCase()))
..foo('Hello'); //HELLO
}
However you will need some extra boilerplate to access instance.
Use type Function:
class A {
final Function h
A(this.h);
void handleLoad(String loadResult) { h(loadResult); }
}
Or
class A {
final Function handleLoad;
A(this.handleLoad);
}
A a = new A((String loadResult){
//do smth.
});

Resources