-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 51ea3f1
Showing
11 changed files
with
422 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
# Created by https://www.toptal.com/developers/gitignore/api/dart,visualstudiocode | ||
# Edit at https://www.toptal.com/developers/gitignore?templates=dart,visualstudiocode | ||
|
||
### Dart ### | ||
# See https://www.dartlang.org/guides/libraries/private-files | ||
|
||
# Files and directories created by pub | ||
.dart_tool/ | ||
.packages | ||
build/ | ||
# If you're building an application, you may want to check-in your pubspec.lock | ||
pubspec.lock | ||
|
||
# Directory created by dartdoc | ||
# If you don't generate documentation locally you can remove this line. | ||
doc/api/ | ||
|
||
# dotenv environment variables file | ||
.env* | ||
|
||
# Avoid committing generated Javascript files: | ||
*.dart.js | ||
*.info.json # Produced by the --dump-info flag. | ||
*.js # When generated by dart2js. Don't specify *.js if your | ||
# project includes source files written in JavaScript. | ||
*.js_ | ||
*.js.deps | ||
*.js.map | ||
|
||
.flutter-plugins | ||
.flutter-plugins-dependencies | ||
|
||
### Dart Patch ### | ||
# dotenv environment variables file | ||
.env | ||
|
||
### VisualStudioCode ### | ||
.vscode/* | ||
!.vscode/settings.json | ||
!.vscode/tasks.json | ||
!.vscode/launch.json | ||
!.vscode/extensions.json | ||
*.code-workspace | ||
|
||
# Local History for Visual Studio Code | ||
.history/ | ||
|
||
### VisualStudioCode Patch ### | ||
# Ignore all local history of files | ||
.history | ||
.ionide | ||
|
||
# Support for Project snippet scope | ||
!.vscode/*.code-snippets | ||
|
||
# End of https://www.toptal.com/developers/gitignore/api/dart,visualstudiocode |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
{ | ||
// Use IntelliSense to learn about possible attributes. | ||
// Hover to view descriptions of existing attributes. | ||
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 | ||
"version": "0.2.0", | ||
"configurations": [ | ||
{ | ||
"name": "reference_wrapper", | ||
"program": "example/reference_wrapper_example.dart", | ||
"request": "launch", | ||
"type": "dart" | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
## 1.0.0 | ||
|
||
- Initial version of the package. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
BSD 3-Clause License | ||
|
||
Copyright (c) 2021, Márton Matuz | ||
All rights reserved. | ||
|
||
Redistribution and use in source and binary forms, with or without | ||
modification, are permitted provided that the following conditions are met: | ||
|
||
1. Redistributions of source code must retain the above copyright notice, this | ||
list of conditions and the following disclaimer. | ||
|
||
2. Redistributions in binary form must reproduce the above copyright notice, | ||
this list of conditions and the following disclaimer in the documentation | ||
and/or other materials provided with the distribution. | ||
|
||
3. Neither the name of the copyright holder nor the names of its | ||
contributors may be used to endorse or promote products derived from | ||
this software without specific prior written permission. | ||
|
||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | ||
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | ||
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE | ||
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR | ||
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER | ||
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, | ||
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | ||
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
`Reference Wrapper` is a Dart package which simulates `pass by reference` feature using Wrapper class which holds the value. | ||
|
||
# Usage | ||
> Use this package if you are calling a function that needs to modify its arguments | ||
## There are two ways to read / write the value | ||
|
||
```dart | ||
var x = Ref<num?>(null); | ||
var read1 = x.ref; // first way to read | ||
var read2 = x(); // second way to read | ||
x.ref = 10; // first way to write | ||
x(10); // second way to write | ||
``` | ||
|
||
## Example | ||
```dart | ||
void twice(Ref<num> x, Ref<num> y) { | ||
x.ref *= 2; | ||
y.ref *= 2; | ||
} | ||
void test() { | ||
var x = Ref<num>(5); | ||
var y = Ref<num>(7); | ||
twice(x, y); | ||
print(x.ref); // 10 | ||
print(y.ref); // 14 | ||
} | ||
``` | ||
|
||
## Advanced Example | ||
```dart | ||
void twice(Ref<num> x, Ref<num> y) { | ||
x(x() * 2); | ||
y(y() * 2); | ||
} | ||
void test() { | ||
var x = Ref<num>(5); | ||
var y = Ref<num>(7); | ||
twice(x, y); | ||
print(x()); // 10 | ||
print(y()); // 14 | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
# This file configures the static analysis results for your project (errors, | ||
# warnings, and lints). | ||
# | ||
# This enables the 'recommended' set of lints from `package:lints`. | ||
# This set helps identify many issues that may lead to problems when running | ||
# or consuming Dart code, and enforces writing Dart using a single, idiomatic | ||
# style and format. | ||
# | ||
# If you want a smaller set of lints you can change this to specify | ||
# 'package:lints/core.yaml'. These are just the most critical lints | ||
# (the recommended set includes the core lints). | ||
# The core lints are also what is used by pub.dev for scoring packages. | ||
|
||
include: package:lints/recommended.yaml | ||
|
||
# Uncomment the following section to specify additional rules. | ||
|
||
# linter: | ||
# rules: | ||
# - camel_case_types | ||
|
||
# analyzer: | ||
# exclude: | ||
# - path/to/excluded/files/** | ||
|
||
# For more information about the core and recommended set of lints, see | ||
# https://dart.dev/go/core-lints | ||
|
||
# For additional information about configuring this file, see | ||
# https://dart.dev/guides/language/analysis-options |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import 'package:reference_wrapper/reference_wrapper.dart'; | ||
|
||
void twice(Ref<num> x, Ref<num> y) { | ||
x.ref *= 2; | ||
y.ref *= 2; | ||
} | ||
|
||
void twiceAdvanced(Ref<num> x, Ref<num> y) { | ||
x(x() * 2); | ||
y(y() * 2); | ||
} | ||
|
||
void main() { | ||
var x = Ref<num>(5); | ||
var y = Ref<num>(7); | ||
|
||
twice(x, y); | ||
print(x.ref); // 10 | ||
print(y.ref); // 14 | ||
|
||
twiceAdvanced(x, y); | ||
print(x()); // 20 | ||
print(y()); // 28 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
library reference_wrapper; | ||
|
||
export 'src/reference_wrapper.dart'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
/// Wraps an arbitary object, if caller method pass [Ref] to callee method, then | ||
/// any changes made on [Ref.ref] will affect caller too. | ||
/// | ||
/// There are two ways to read / write the value: | ||
/// | ||
/// ```dart | ||
/// var x = Ref<num?>(null); | ||
/// x.ref; // first way to read | ||
/// x(); // second way to read | ||
/// | ||
/// x.ref = 10; // first way to write | ||
/// x(15); // second way to write | ||
/// ``` | ||
/// | ||
/// **Example:** | ||
/// ```dart | ||
/// void twice(Ref<num> x, Ref<num> y) { | ||
/// x.ref *= 2; | ||
/// y.ref *= 2; | ||
/// } | ||
/// | ||
/// void test() { | ||
/// var x = Ref<num>(5); | ||
/// var y = Ref<num>(7); | ||
/// | ||
/// twice(x, y); | ||
/// print(x.ref); // 10 | ||
/// print(y.ref); // 14 | ||
/// } | ||
/// ``` | ||
/// | ||
/// **Advanced Example:** | ||
/// ```dart | ||
/// void twiceAdvanced(Ref<num> x, Ref<num> y) { | ||
/// x(x() * 2); | ||
/// y(y() * 2); | ||
/// } | ||
/// | ||
/// void test() { | ||
/// var x = Ref<num>(5); | ||
/// var y = Ref<num>(7); | ||
/// | ||
/// twiceAdvanced(x, y); | ||
/// print(x()); // 10 | ||
/// print(y()); // 14 | ||
/// } | ||
/// ``` | ||
abstract class Ref<T> { | ||
Ref._(); | ||
|
||
factory Ref(T ref) = _Ref; | ||
|
||
T get ref; | ||
|
||
set ref(T ref); | ||
|
||
T call([T ref]); | ||
|
||
Type get genericRuntimeType; | ||
|
||
static Type genericStaticType<S>(Ref<S> ref) => S; | ||
|
||
@override | ||
String toString() => 'Ref(ref: $ref)'; | ||
} | ||
|
||
class _Undefined {} | ||
|
||
class _Ref<T> extends Ref<T> { | ||
@override | ||
T ref; | ||
|
||
_Ref(this.ref) : super._(); | ||
|
||
@override | ||
T call([Object? ref = _Undefined]) { | ||
return identical(ref, _Undefined) ? this.ref : this.ref = ref as T; | ||
} | ||
|
||
@override | ||
Type get genericRuntimeType => T; | ||
|
||
@override | ||
bool operator ==(Object other) { | ||
if (identical(this, other)) { | ||
return true; | ||
} | ||
|
||
return other is _Ref<T> && | ||
other.genericRuntimeType == genericRuntimeType && | ||
other.ref == ref; | ||
} | ||
|
||
@override | ||
int get hashCode => ref.hashCode; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
name: reference_wrapper | ||
description: A Dart package which simulates pass by reference feature. | ||
version: 1.0.0 | ||
repository: https://github.com/maRci002/reference_wrapper | ||
homepage: https://github.com/maRci002/reference_wrapper | ||
|
||
environment: | ||
sdk: '>=2.12.0 <3.0.0' | ||
|
||
dev_dependencies: | ||
lints: ^1.0.0 | ||
test: ^1.16.0 |
Oops, something went wrong.