Dart Language Extension Method - flutter

I am reading extension method in dart official documentation, and reached the bottom of the document. Here, the documentation mentioned the line List<T> operator -() => reversed.toList(); as a extension method on List<T>.
Here is the complete code.
extension MyFancyList<T> on List<T> {
int get doubleLength => length * 2;
List<T> operator -() => reversed.toList();
List<List<T>> split(int at) => [sublist(0, at), sublist(at)];
}
What does mean operator -(), operator +(List<T> t),and operator *(List<T> t) and how can I use these as extension method on List?

It means you can actually use it as an operator on the list. For example:
final list = [1,2,3,4,5];
print(-list);
prints [5,4,3,2,1]
Personally, I would not implement a revers like this. I think it would be quite unclear for other developers, while the reversed method is very clear on what it does. However, I understand it is just an example on how to define an operator in an extension.
operator +(List<T> t) and operator *(List<T> t) is between two lists. For example:
final a = [1,2];
final b = [3,4];
print(a + b);
prints [1,2,3,4]

Related

What does " : " after Board.fromDatabase(DataSnapshot snapshot) mean. I want to undestand how " : " works here and what is it. If possible by exmaple [duplicate]

This code is from flutter gallery and i'm trying to understanding and adapting it. I would know what this syntax means:
class DemoItem<T> {
DemoItem({
this.valueName,
this.hintName,
this.valueSurname,
this.hintSurname,
this.builder,
this.valueToString
}) : textController = new TextEditingController(text: valueToString(valueName));
Especially i would know what means the colon after the constructor and if there is a way to define another TextEditingController, in addition to the one already defined.
The part after : is called "initializer list". It is a ,-separated list of expressions that can access constructor parameters and can assign to instance fields, even final instance fields. This is handy to initialize final fields with calculated values.
The initializer list is also used to call other constructors like : ..., super('foo').
Since Dart version 2.0 the initializer list also supports assert(...) which is handy to check parameter values.
The initializer list can't read from this because the super constructors need to be completed before access to this is valid, but it can assign to this.xxx.
Pointing out as mentioned in the comments by user693336:
This also means the initializer list is executed before the constructor body. Also the initializer lists of all superclasses are executed before any of the constructor bodies are executed.
Example (copied from https://github.com/dart-lang/language/issues/1394):
class C {
final int x;
final int y;
C(this.x) : y = x + 1;
}
To elaborate on other answers and to complete the syntax, it is also possible to have a real body for the constructor along with initializer code
NonNegativePoint(this.x, this.y) : assert(x >= 0), assert(y >= 0) {
print('I just made a NonNegativePoint: ($x, $y)');
}
^ Here the assertions happen before the execution of the body
Another use case is to assign values to final fields before body executes
final num x;
final num y;
Point.fromJson(Map<String, num> json) : x = json['x'], y = json['y'] {
print('In Point.fromJson(): ($x, $y)');
}

How do you sort a list by bool in Dart

How do you sort a List in dart based on a bool value, the compareTo method doesn't work with bool. I want true values to appear at the top of the list.
You can define your own compare function for bool and pass it to the sort method of List.
Example with booleans as your bool List:
booleans.sort((a, b) {
if(b) {
return 1;
}
return -1;
});
This example tells the sort method that true elements should be sorted higher than false elements.
I just got the same issue, I solve it using the .toString() function. So it's sort separating the 0's to the 1's.
In this case I got a list of forms, some are synced and others no.
Here's my code:
final forms = state.forms
..sort(
(a, b) => a.synced.toString().compareTo(b.synced.toString()),
);
Hope it works for you.

Can I implement operator overrides in typescript?

Say I am creating a Dictionary class in typescript. Is there a way to have operator overrides so I can define the operator "[string]" instead of having to use the function get(string)?
No, you cannot have operator overloading. JavaScript does not have a notion of this.
You can override toString, though:
class Thing {
toString() {
return 'I am a Thing!';
}
}
var x = new Thing();
console.log('X says ' + x); // Prints "X says I am a Thing!"

dart, overloading [] operator?

Is it possible to overload the index[] syntax for a collection/iterable type? for example I am writing a SortedList<T> class that wraps around a standard List<T>. and I'd just like to pass on the usage from my sorted list straight down to the underlying list object. I thought I'd read about this on the dart language tour but I can't find it now.
Dart editor seems pretty happy with:
operator [](int i) => list[i]; // get
operator []=(int i, int value) => _list[i] = value; // set
Try it in DartPad
Yes you can override operators. Like this:
T operator [](int index) => items[index];
But you can't overload anything in dart. Because Dart does not support operator or function overloading. That means your class can't have two function with same name but different parameters.

overload operator = in c#

Is it possible to overload operator = in c#?
when i call =, i just want to copy properties, rather than making the left hand reference to refer another instance.
The answer is no:
Note that the assignment operator itself (=) cannot be overloaded. An assignment always performs a simple bit-wise copy of a value into a variable.
And even if it was possible, I wouldn't recommend it. Nobody who reads the code is going to think that there's ANY possibility that the assignment operator's been overloaded. A method to copy the object will be much clearer.
You can't overload the = operator. Furthermore, what you are trying to do would entirely change the operator's semantics, so in other words is a bad idea.
If you want to provide copy semantics, provide a Clone() method.
Why not make a .CopyProperties(ByVal yourObject As YourObject) method in the object?
Are you using a built-in object?
We can Overload = operator but not directly.
using System;
class Over
{
private int a;
public Over(int a )
{
this.a = a;
}
public int A { get => a; }
public static implicit operator int(Over obj)
{
return obj.A;
}
}
class yo
{
public static void Main()
{
Over over = new Over(10);
int a = over;
}
}
Here when you call that operator overloading method , it is calling = operator itself to convert that to int. You cannot directly overload "=" but this way of code means the same.