Skip to content

Commit

Permalink
Merge pull request #619 from ntrel/mix-opcall
Browse files Browse the repository at this point in the history
Fix Issue 12241 - Document change to static opCall
  • Loading branch information
AndrejMitrovic authored and AndrewEdwards committed Aug 16, 2014
1 parent 6725aa1 commit 8719bac
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 18 deletions.
24 changes: 24 additions & 0 deletions changelog.dd
Expand Up @@ -274,6 +274,7 @@ $(LI $(RELATIVE_LINK2 deprecate_unorderd_compares, Deprecate unordered floating
$(LI $(RELATIVE_LINK2 deprecate_floating_properties, Deprecate $(D .min) property for floating-point types.))
$(LI $(RELATIVE_LINK2 ctfe_overlapped_field, CTFE can handle overlapped union fields.))
$(LI $(RELATIVE_LINK2 get_alias_this, Add a new trait getAliasThis.))
$(LI $(RELATIVE_LINK2 mixing-struct-opcall, Mixing struct constructors and static opCall is no longer allowed.))
)

$(BUGSTITLE Library Changes,
Expand Down Expand Up @@ -548,6 +549,29 @@ $(LI $(LNAME2 get_alias_this, Add a new trait getAliasThis:)
)
)

$(LI $(LNAME2 mixing-struct-opcall, Mixing struct constructors and static opCall is no longer allowed):
$(P This was not implemented correctly and caused ambiguities.)
$(P
Example:
---
struct S
{
this(int i) {}

static S opCall() // disallowed due to constructor
{
return S.init;
}
}
---
)
$(P Note: $(D static opCall) can be used to simulate struct
constructors with no arguments, but this is not recommended
practice. Instead, the preferred solution is to use a factory
function to create struct instances.
)
)

)

$(BUGSTITLE Library Changes,
Expand Down
1 change: 0 additions & 1 deletion doc.ddoc
Expand Up @@ -395,7 +395,6 @@ SCINI=<pre class="scini">$(NOTRANSLATE $0)</pre>
CONSOLE=<pre class="console">$(NOTRANSLATE $0)</pre>
MODDEFFILE=<pre class="moddeffile">$(NOTRANSLATE $0)</pre>
CODE_HIGHLIGHT=$(B $(I $0))

MDASH=<nobr>&#x200A;&mdash;&#x200A;</nobr>

NEWS=http://digitalmars.com/drn-bin/wwwnews?$1/$+
Expand Down
55 changes: 38 additions & 17 deletions operatoroverloading.dd
Expand Up @@ -60,7 +60,7 @@ $(H2 $(LNAME2 Unary, Unary Operator Overloading))
)
)

$(P For example, in order to overload the - (negation) operator for struct S, and
$(P For example, in order to overload the $(D -) (negation) operator for struct S, and
no other operator:)

---
Expand Down Expand Up @@ -414,7 +414,7 @@ struct S {

$(H2 $(LNAME2 FunctionCall, Function Call Operator Overloading $(D f())))

$(P The function call operator, (), can be overloaded by
$(P The function call operator, $(D ()), can be overloaded by
declaring a function named $(CODE opCall):
)

Expand All @@ -437,23 +437,9 @@ $(H2 $(LNAME2 FunctionCall, Function Call Operator Overloading $(D f())))
were a function.
)

$(P $(CODE static opCall) also works as expected for function call operator with
type names.
)

-------
struct Double {
$(CODE_HIGHLIGHT static) int $(CODE_HIGHLIGHT opCall)(int x) { return x * 2; }
}
void test() {
int i = Double(2);
assert(i == 4);
}
-------

$(P Note that merely declaring $(D opCall) automatically disables
$(XLINK2 struct.html#StructLiteral, struct literal) syntax.
To avoid the limitation, you need to also declare $(XLINK2 Struct-Constructor,
To avoid the limitation, you need to also declare a $(XLINK2 struct.html#Struct-Constructor,
constructor) so that it takes priority over $(D opCall) in $(D Type(...)) syntax.
)

Expand All @@ -469,6 +455,41 @@ $(H2 $(LNAME2 FunctionCall, Function Call Operator Overloading $(D f())))
assert(result == 50);
}
-------

$(H3 $(LNAME2 static-opcall, Static opCall))

$(P $(CODE static opCall) also works as expected for a function call operator with
type names.
)

-------
struct Double {
$(CODE_HIGHLIGHT static) int $(CODE_HIGHLIGHT opCall)(int x) { return x * 2; }
}
void test() {
int i = Double(2);
assert(i == 4);
}
-------

$(P Mixing struct constructors and $(D static opCall) is not allowed.)

---
struct S
{
this(int i) {}
static S opCall() // disallowed due to constructor
{
return S.init;
}
}
---

$(P Note: $(D static opCall) can be used to simulate struct
constructors with no arguments, but this is not recommended
practice. Instead, the preferred solution is to use a factory
function to create struct instances.
)

$(H2 $(LNAME2 Assignment, Assignment Operator Overloading))

Expand Down

0 comments on commit 8719bac

Please sign in to comment.