On Saturday, 22 November 2014 at 02:37:21 UTC, Eric wrote:
I know I can make a class immutable, but the problem is I want
to constrain a template parameter to only immutable types,
and I want to use class types.
template Foo(T : immutable Object)
Accepts immutable(Object) and other immutable
On Saturday, 22 November 2014 at 09:57:55 UTC, anonymous wrote:
On Saturday, 22 November 2014 at 02:37:21 UTC, Eric wrote:
I know I can make a class immutable, but the problem is I want
to constrain a template parameter to only immutable types,
and I want to use class types.
template Foo(T :
On Saturday, 22 November 2014 at 15:00:00 UTC, Eric wrote:
Yes, but if I don't declare the class T as immutable, I don't
think this constraint will work.
You're mistaken. It works just fine.
class X /* not immutable */
{
private int x;
this(int x) pure { this.x = x; }
}
template
On Saturday, 22 November 2014 at 17:06:29 UTC, anonymous wrote:
On Saturday, 22 November 2014 at 15:00:00 UTC, Eric wrote:
Yes, but if I don't declare the class T as immutable, I don't
think this constraint will work.
You're mistaken. It works just fine.
class X /* not immutable */
{
On Saturday, 22 November 2014 at 17:40:42 UTC, Eric wrote:
Yes, but this is what I really want:
class X(T : immutable Object)
{
private T x;
this(T x) pure { this.x = x; }
}
class Y
{
private int x;
this(int x) pure { this.x = x; }
}
void main()
{
immutable(Y) y = new
But I'm not sure if maybe I changed to much about it.
My point is, that I think it's generally a good idea to be
flexible when possible, and not make (im)mutability demands
unless actually necessary.
You may know the following, but I feel like there may be some
confusion about it: Note that
On Saturday, November 22, 2014 14:59:58 Eric via Digitalmars-d-learn wrote:
On Saturday, 22 November 2014 at 09:57:55 UTC, anonymous wrote:
On Saturday, 22 November 2014 at 02:37:21 UTC, Eric wrote:
I know I can make a class immutable, but the problem is I want
to constrain a template
immutable class X
{
private int x;
this(int x) { this.x = x; }
...
override size_t toHash(); // error: can't override mutable
method
}
Since toHash() cannot be overridden in an immutable class,
is there a work-around?
In other words,
immutable X x1 = new immutable X(5);
On Saturday, 22 November 2014 at 00:24:39 UTC, Eric wrote:
immutable class X
{
private int x;
this(int x) { this.x = x; }
...
override size_t toHash(); // error: can't override mutable
method
}
Since toHash() cannot be overridden in an immutable class,
is there a work-around?
But think about if enforced immutability is really what you
want.
You don't need to mark the fields immutable to be able to
construct immutable Xs. A `pure` constructor is handy, as it can
construct both mutable and immutable objects.
class X
{
private int x;
this(int x) pure {
On 11/21/14 7:24 PM, Eric wrote:
immutable class X
{
private int x;
this(int x) { this.x = x; }
...
override size_t toHash(); // error: can't override mutable method
}
Since toHash() cannot be overridden in an immutable class,
is there a work-around?
Kind of, but I don't
11 matches
Mail list logo