digitalmars.D.learn - UFCS with constructors
- bearophile (20/20) Nov 06 2013 import std.typecons: Typedef;
- qznc (5/23) Nov 06 2013 Operator precedence of "." is higher than unary minus. That
- bearophile (14/16) Nov 06 2013 It's to create a differently named type, useful for stronger
- bearophile (14/15) Nov 07 2013 You are right, I didn't know it, so Typedef and constructors are
- =?UTF-8?B?QWxpIMOHZWhyZWxp?= (20/40) Nov 06 2013 I would be very surprised if unary "-" produced a different type from
- Maxim Fomin (7/10) Nov 06 2013 Operator does not produce type, it produces value of expression,
- =?UTF-8?B?QWxpIMOHZWhyZWxp?= (6/20) Nov 06 2013 I still argue that the expression -expr must have the same type as expr.
- Maxim Fomin (4/17) Nov 06 2013 I think that reason for such behavior is the way used defined
- Dicebot (3/6) Nov 06 2013 What about other possible reason - "Typedef implementation
- bearophile (5/6) Nov 06 2013 So do you suggest to open some enhancement request/bug report on
- Dicebot (4/10) Nov 06 2013 Sure. get enough such reports and we may even get it back as
import std.typecons: Typedef; alias Foo = Typedef!double; void main() { auto a1 = Foo(1); pragma(msg, typeof(a1)); auto a2 = 1.Foo; pragma(msg, typeof(a2)); auto a3 = Foo(-1); pragma(msg, typeof(a3)); auto a4 = -1.Foo; pragma(msg, typeof(a4)); } It prints: Typedef!(double, nan) Typedef!(double, nan) Typedef!(double, nan) double Is this expected/acceptable/good? Bye, bearophile
Nov 06 2013
On Wednesday, 6 November 2013 at 11:04:05 UTC, bearophile wrote:import std.typecons: Typedef; alias Foo = Typedef!double; void main() { auto a1 = Foo(1); pragma(msg, typeof(a1)); auto a2 = 1.Foo; pragma(msg, typeof(a2)); auto a3 = Foo(-1); pragma(msg, typeof(a3)); auto a4 = -1.Foo; pragma(msg, typeof(a4)); } It prints: Typedef!(double, nan) Typedef!(double, nan) Typedef!(double, nan) double Is this expected/acceptable/good?Operator precedence of "." is higher than unary minus. That should be the explanation, why the fourth output is different than the others. However, what is Typedef for?
Nov 06 2013
qznc:Operator precedence of "." is higher than unary minus.Is this good?However, what is Typedef for?It's to create a differently named type, useful for stronger static typing, to increase code clarity and avoid some bugs. If you have a function like this: double foo(in double x, in double k) pure nothrow You can give it swapped arguments (also because D still lacks named arguments). But if you define a: alias Coordinate = Typedef!double; Coordinate foo(in Coordinate x, in double k) pure nothrow You will have less mistakes. Currently Typedef has some bugs. Bye, bearophile
Nov 06 2013
qznc:Operator precedence of "." is higher than unary minus.You are right, I didn't know it, so Typedef and constructors are not to blame: double foo(in double x) { assert (x >= 0); return x; } void main() { assert(-1.foo == -1); } Is this a good design of the operator precedences? Is this worth changing/fixing? Bye, bearophile
Nov 07 2013
On 11/06/2013 03:04 AM, bearophile wrote:import std.typecons: Typedef; alias Foo = Typedef!double; void main() { auto a1 = Foo(1); pragma(msg, typeof(a1)); auto a2 = 1.Foo; pragma(msg, typeof(a2)); auto a3 = Foo(-1); pragma(msg, typeof(a3)); auto a4 = -1.Foo; pragma(msg, typeof(a4)); } It prints: Typedef!(double, nan) Typedef!(double, nan) Typedef!(double, nan) double Is this expected/acceptable/good? Bye, bearophileI would be very surprised if unary "-" produced a different type from the operand: import std.typecons: Typedef; alias Foo = Typedef!double; void main() { auto a = 1.Foo; auto b = -a; static assert (is (typeof(a) == typeof(b))); // FAILS! } After all, we are used to hidden bugs based on that expectation: ;) void main() { uint a = 1; auto b = -a; assert(b == uint.max); // WT? static assert(is (typeof(b) == uint)); // <-- the reason } Seriously though, yeah, unary "-" must return Typedef!(double, nan). Ali
Nov 06 2013
On Wednesday, 6 November 2013 at 17:10:34 UTC, Ali Çehreli wrote:I would be very surprised if unary "-" produced a different type from the operand: AliOperator does not produce type, it produces value of expression, and type of expression happens not to be the type you expected. But such expectations need not correspond to language rules (try to think from from language laywer perspective). In bearophile case, I guess Typedef!double overloads unary operator which returns double which is primary reason for such behavior.
Nov 06 2013
On 11/06/2013 09:46 AM, Maxim Fomin wrote:On Wednesday, 6 November 2013 at 17:10:34 UTC, Ali Çehreli wrote:Thanks. That's what I meant. :)I would be very surprised if unary "-" produced a different type from the operand: AliOperator does not produce type, it produces value of expression, and type of expression happens not to be the type you expected.But such expectations need not correspond to language rules (try to think from from language laywer perspective).I still argue that the expression -expr must have the same type as expr.In bearophile case, I guess Typedef!double overloads unary operator which returns double which is primary reason for such behavior.That's what I deduced from qznc's post and tried to mean that such behavior would be confusing to programmers. Ali
Nov 06 2013
On Wednesday, 6 November 2013 at 18:02:32 UTC, Ali Çehreli wrote:I think that reason for such behavior is the way used defined operator overloading functions are implemented, not the language per se, so programmers confuse themselves.But such expectations need not correspond to language rules (try tothink fromfrom language laywer perspective).I still argue that the expression -expr must have the same type as expr.In bearophile case, I guess Typedef!double overloads unary operator which returns doublewhich isprimary reason for such behavior.That's what I deduced from qznc's post and tried to mean that such behavior would be confusing to programmers. Ali
Nov 06 2013
On Wednesday, 6 November 2013 at 18:16:04 UTC, Maxim Fomin wrote:I think that reason for such behavior is the way used defined operator overloading functions are implemented, not the language per se, so programmers confuse themselves.What about other possible reason - "Typedef implementation sucks"? ;)
Nov 06 2013
Dicebot:"Typedef implementation sucks"? ;)So do you suggest to open some enhancement request/bug report on Typedef? Bye, bearophile
Nov 06 2013
On Wednesday, 6 November 2013 at 21:57:47 UTC, bearophile wrote:Dicebot:Sure. get enough such reports and we may even get it back as language feature :) (I think getting Typedef to act properly in all cases is damn hard task to do)"Typedef implementation sucks"? ;)So do you suggest to open some enhancement request/bug report on Typedef? Bye, bearophile
Nov 06 2013