How to use factory const constructors? - constructor

To test my understanding of Dart, I made a 2D immutable vector that stores not only its x and y components, but also its angle and length. They are computed only at construction out of the x and y values. I soon realized that final fields need to be set with initializer lists or the this-parameter shortcut, which don't allow for much computed values. Like this answer points out, I had to use a factory constructor to create my vector out of its x and y components. The factory constructor then validates input and computes the length and angle before calling the private constructor.
import 'dart:math';
class ImmutableVector {
// private fields
final double _x;
final double _y;
final double _angle;
final double _length;
// getters
double get x => _x;
double get y => _y;
double get angle => _angle;
double get length => _length;
/// Constructs a vector out of cartesian components.
///
/// Converts both arguments to doubles.
/// Null values will be treated as zeroes.
factory ImmutableVector.xy(num x, num y) {
x ??= 0.0;
y ??= 0.0;
x = x.toDouble();
y = y.toDouble();
var angle = atan2(y, x) % (2*PI);
var length = sqrt(pow(x, 2) + pow(y, 2));
return new ImmutableVector._raw(x, y, angle, length);
}
/// Constructs a vector by setting the fields directly.
const ImmutableVector._raw(this._x, this._y, this._angle, this._length);
}
But it came to my attention that I could not make the factory constructor const, because const factories can only be redirecting constructors. Is there absolutely no way to make my vector have code in its constructor and still have it an immutable with const constructors? And if so, why?
I'll also mention that I used to throw errors if the values passed were null, but I made it default to zero so I could actually use initializer lists. I then tried to do this, and turns out it works when the constructor isn't a factory:
ImmutableVector.xy(num x, num y) : this._raw(
x = (x ?? 0.0).toDouble(),
y = (y ?? 0.0).toDouble(),
atan2(y, x) % (2*PI),
sqrt(pow(x, 2) + pow(y, 2)));
But as soon as I try to make it const, it tells me that the code in the initializer list contains non-compile-time constants.
The only immutable vector I could find in dart was here on GitHub, and it doesn't do any kind of null-validation or computation on constructor parameters, relying entirely on the fact that methods will break at some point on null-vectors. It also has a constructor that creates a unit vector out of another one with poor performance and repetition thanks to the obligatory initializer list:
const Vector.unit(final num x, final num y, final num z) :
this(x / PMath.len(x, y, z), y / PMath.len(x, y, z), z / PMath.len(x, y, z));
So what should be my conclusion? Did I miss a feature that makes this possible, or should I just give up on using const for this class?

Dart doesn't execute Dart code during compilation. This is the reason const constructors can't have a body and why there is no other way to work around this limitation.
If you want to execute code when an instance is created, just don't make use of const. Const isn't that important in Dart anyway. There were even discussions to remove it from the language because the benefits are not big enough.

Related

function handle to function with multiple output and input variables in filedatastore

I am trying to build a fileDatastore from multiple large files using a custom read function.
This should work like
fds = fileDatastore(location, 'ReadFcn', #fcn)
I use a cell array containing several fullfile strings for location, which should be fine.
I simply can't figure out how to handle the read function which has multiple input and output variables and looks like this:
[A, B, C] = function(filestring, x, y, z)
How can I make this work?
Thanks in advance!
You can use an "anonymous function" to do part of this. An anonymous function lets you adapt the prototype of the function, "binding" in values, so one piece of the puzzle is to do this:
x = 7; y = 42; z = -1;
fcn = #(filename) myfunction(filename, x, y, z);
This makes a function fcn which you can call like fcn('somefile') and the effect is myfunction('somefile', 7, 42, -1).
Unfortunately, there's a twist - fileDatastore needs a function returning only a single output. This is not something you can fix with an anonymous function, so you'll need to write a function in a MATLAB code file that does something like this perhaps:
function out = wrapMyFunction(filename, x, y, z)
[A,B,C] = myfunction(filename, x, y, z);
out = {A,B,C};
end
And then use
fcn = #(filename) wrapMyFunction(filename, x, y, z);

Explanation to why two constructors are required

Unfortunately, I do not feel confident with my understanding of default constructors.
I have searched extensively to find a resource that provides an explanation to adhere to my personal learning curve of the Java language. However, upon completing an assignment, I feel I may not be meeting the assignment criteria due to my own feeling of redundancy to need for a default constructor. This is why i feel like i am misinterpreting the concept of different types of constructors all together.
I have created two constructors as the assignment requires. One that takes in no parameters and initializes instance variables to a default value. And another that takes in parameters to give values to the object variables when the new object is created in the main method.
Why am I creating a default constructor for the object if the default is never used in the main method? Below is a sample of my code:
public class Circle {
private double x; // declaring variable to hold value of x coordinate
private double y; // Variable to hold value of y coordinate
private double r; // Variable to hold value of the radius of a circle
/* default constructor */
Circle() {
x = 0.0;
y = 0.0;
r = 0.0;
}
/* constructor takes in three parameters and sets values for variables x, y, and r */
public Circle(double x, double y, double r) {
this.x = x;
this.y = y;
this.r = r;
}
// test class created for main method
public class TestCircle {
public static void main (String[] args){
Circle c1 = new Circle(2.0,3.0,9.0);
System.out.println();
System.out.println(" A circle object has been created with the following attributes:");
c1.printAttributes();
System.out.println();
System.out.println("The circle is tested for the maximum radius of 8.0...");
c1.setRadius(8.0);
System.out.println();
System.out.println("... since the radius is more than the allowable maximum, the new attributes for the Circle are:");
c1.printAttributes();
System.out.println();
System.out.println("The area of the Circle is " + c1.area());
System.out.println("The Circumference of the circle is " + c1.circumference());
System.out.println();
System.out.println("The origin of the circle is now moved by a specified amount...");
c1.move(6,-7);
System.out.println();
System.out.println("The new attributes of the circle are:");
c1.printAttributes();
System.out.println();
System.out.println("Testing if the point (10,-20) is inside the circle...");
System.out.println();
if (c1.isInside(10,-20)){
System.out.println("The point (10,-20) is inside the circle");
}
else {
System.out.println("The point (10,-20) is not inside the circle");
}
} // end of main
} // end of class
If you don’t use it you should delete it. Sometimes you will need to create empty objects in order to set attributes a posteriori, but if you are not using it at all there is no point to have it
The point of making default constructors is sometimes for back end stuff and is considered a "good programming practice" no you don't use the default constructor here in your main and in fact your code would run just fine with no default constructor comment it out and re run your tester you will see it works fine.

What happens when you pass an expression to a function which has passing by value-result?

I'm attending a course of principles of programming languages and there's this exercise where I'm supposed to tell what gets printed by this program:
{
int x=2;
void A (val/res int y)
{
x++;
write(y);
y=y+2;
}
A(x)
A(x+1)
write (x);
}
A is a function with value/result parameters passing, so right before returning it should copy the final value of its formal parameter (y) in the actual parameter. When A first gets called its actual parameter is x, so there's no problem there. However, the second call to A has x+1 as the actual parameter.
What does that mean? Maybe the final value of y gets lost because there's no variable where to copy it? Or maybe I should consider it like an equation, so if the final value of y is 7 I get that x + 1 = 7, and then the value of x is 6?
It means the value of the argument is copied to y:
When x=2, A(x) copies 2 to y at the start of A
When x=4, A(x+1) copies the value of x+1, or 5, to y at the start of A
However, as you pointed, out, passing x+1 for a value/result parameter is problematic, and I would expect any language supporting this type of parameter would not consider it legal, for just the reason you cite. If it is considered legal, how it is accomplished would be up to the language definition; I do not believe there is a standard way to handle this.

How to get the output from arrow & curry function in javascript

Here is my code. I did not get the output that I want. It should hand over 50 but it does'nt. Someone please help me to fix the bug?
const checkNum = entity => typeof entity === 'string' ? parseInt(entity) : entity;
const multiply = (x , y) => x => y => x * y;
const multiOne = (ar, gr) => ar.map(checkNum).map(multiply);
multiOne([5, 10]);
map is a one-to-one operation, so for your array it produces another array. The second argument is not what you think it is.
reduce is a many-to-one operation, taking and array and reducing / folding it by repeatedly applying an operation to two elements at a time (two elements from the array at first, then the result of the previous operation combined with another element of the array).
const multiply = (x, y) => x * y;
const multiOne = (ar) => ar.reduce(multiply);
console.log(multiOne([5, 10]));
You also seem to want partially applied function, but you are currying too much. The standard functions in ES6 don't use currying a lot, it is more common in actual FP languages (e.g. Haskell).

Does this higher order function have a name?

I see this pattern everywhere in my code, and in libraries, yet there appears to be no name or abstraction of it that I can find anywhere.
Example (pseudocode)
T foo( T x, void f(T&) )
{
T y = x;
f( y );
return y;
}
Basically: Take a value, and a function that transforms that value. Make of a copy of the value, transform it, and return it.
Real-life examples (C++)
T operator+(const T& x, const T& y)
{
T z = x; // Make a copy
operator+=(z, y); // Modify in place
return z;
}
Vector3 Vector3::normalized() const
{
Vector3 x = *this; // Make a copy
x.normalize(); // Modify in place
return x;
}
T sorted(T const& x)
{
T y = x; // Make a copy (yeah, yeah, could have passed by value)
sort( y ); // Modify in place
return y;
}
Basically, you have an in place function (with side-effects) and make an out-of-place function (without side-effects) out of it.
Is there a name for this pattern? Do you know of any libraries or languages that use it? Obviously functional languages won't use it because they don't have referentially opaque functions to begin with.
It's actually what in mathematics and FP is called a composition, because you could express it as mystery_function(x, fun) = fun(copy(x)) instead.
In Design Patterns linguo, it's a wrapper, that wraps the function call with a copy. So I would rather naturally call it a copy wrapper. But I never saw it classified anywhere.