Skip to content

Commit 164f42f

Browse files
author
Max Moiseev
committed
[stdlib] new naming applied to arithmetic operators in the integer prototype
1 parent 8f61c05 commit 164f42f

File tree

1 file changed

+22
-18
lines changed

1 file changed

+22
-18
lines changed

test/Prototypes/Integers.swift.gyb

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,11 @@ class struct(object):
4545
return 'struct(%r)' % self.__dict__
4646

4747
binaryArithmetic = [
48-
struct(operator='+', name='add', llvmName='add', kind='+'),
49-
struct(operator='-', name='subtract', llvmName='sub', kind='-'),
50-
struct(operator='*', name='multiply', llvmName='mul', kind='*'),
51-
struct(operator='/', name='divideBy', llvmName='div', kind='/'),
52-
struct(
53-
operator='%', name='remainderWhenDividedBy', llvmName='rem', kind='/')
48+
struct(operator='+', name='adding', mutatingName='add', firstArg='', llvmName='add', kind='+'),
49+
struct(operator='-', name='subtracting', mutatingName='subtract', firstArg='', llvmName='sub', kind='-'),
50+
struct(operator='*', name='multiplied', mutatingName='multiply', firstArg='by', llvmName='mul', kind='*'),
51+
struct(operator='/', name='divided', mutatingName='divide', firstArg='by', llvmName='div', kind='/'),
52+
struct(operator='%', name='remainder', mutatingName='formRemainder', firstArg='dividingBy', llvmName='rem', kind='/')
5453
]
5554

5655

@@ -133,18 +132,19 @@ public protocol Arithmetic {
133132
// defaulted using an InPlace counterpart, but can be used as an
134133
// optimization hook
135134
@warn_unused_result
136-
func ${x.name}(rhs: Self) -> Self
135+
func ${x.name}(${x.firstArg} rhs: Self) -> Self
137136

138137
// implementation hook
139-
mutating func ${x.name}InPlace(rhs: Self)
138+
mutating func ${x.mutatingName}(${x.firstArg} rhs: Self)
140139
% end
141140
}
142141

143142
extension Arithmetic {
144143
% for x in binaryArithmetic:
145-
public func ${x.name}(rhs: Self) -> Self {
144+
% callLabel = x.firstArg + ': ' if x.firstArg else ''
145+
public func ${x.name}(${x.firstArg} rhs: Self) -> Self {
146146
var lhs = self
147-
lhs.${x.name}InPlace(rhs)
147+
lhs.${x.mutatingName}(${callLabel}rhs)
148148
return lhs
149149
}
150150
% end
@@ -161,15 +161,16 @@ extension SignedArithmetic {
161161
}
162162

163163
% for x in binaryArithmetic:
164+
% callLabel = x.firstArg + ': ' if x.firstArg else ''
164165
@_transparent
165166
@warn_unused_result
166167
public func ${x.operator} <T: Arithmetic>(lhs: T, rhs: T) -> T {
167-
return lhs.${x.name}(rhs)
168+
return lhs.${x.name}(${callLabel}rhs)
168169
}
169170

170171
@_transparent
171172
public func ${x.operator}= <T: Arithmetic>(lhs: inout T, rhs: T) {
172-
lhs.${x.name}InPlace(rhs)
173+
lhs.${x.mutatingName}(${callLabel}rhs)
173174
}
174175
% end
175176

@@ -366,7 +367,7 @@ comment = '''
366367
${comment}
367368
@warn_unused_result
368369
func ${x.name}WithOverflow(
369-
rhs: Self
370+
${x.firstArg} rhs: Self
370371
) -> (partialValue: Self, overflow: ArithmeticOverflow)
371372
% end
372373

@@ -491,9 +492,10 @@ extension FixedWidthInteger {
491492
}
492493

493494
% for x in binaryArithmetic:
495+
% callLabel = x.firstArg + ': ' if x.firstArg else ''
494496
@_transparent
495-
public mutating func ${x.name}InPlace(rhs: Self) {
496-
let (result, overflow) = self.${x.name}WithOverflow(rhs)
497+
public mutating func ${x.mutatingName}(${x.firstArg} rhs: Self) {
498+
let (result, overflow) = self.${x.name}WithOverflow(${callLabel}rhs)
497499
_assertCond(overflow == .none, "overflow in ${x.name}")
498500
self = result
499501
}
@@ -506,7 +508,7 @@ extension FixedWidthInteger {
506508
@warn_unused_result
507509
@_transparent
508510
public func unsafe${capitalize(x.name)}(rhs: Self) -> Self {
509-
let (result, overflow) = self.${x.name}WithOverflow(rhs)
511+
let (result, overflow) = self.${x.name}WithOverflow(${callLabel}rhs)
510512

511513
if (overflow != .none) {
512514
if (_isDebugAssertConfiguration()) {
@@ -555,10 +557,11 @@ extension FixedWidthInteger {
555557
}
556558

557559
% for x in binaryArithmetic:
560+
% callLabel = x.firstArg + ': ' if x.firstArg else ''
558561
% if x.kind != '/':
559562
@warn_unused_result
560563
public func &${x.operator} <T: FixedWidthInteger>(lhs: T, rhs: T) -> T {
561-
return lhs.${x.name}WithOverflow(rhs).partialValue
564+
return lhs.${x.name}WithOverflow(${callLabel}rhs).partialValue
562565
}
563566
% end
564567
% end
@@ -703,13 +706,14 @@ public struct ${Self}
703706
}
704707

705708
% for x in binaryArithmetic:
709+
% callLabel = x.firstArg + ': ' if x.firstArg else ''
706710
/// Return a pair consisting of `self` ${x.operator} `rhs`,
707711
/// truncated to fit if necessary, and a flag indicating whether an
708712
/// arithmetic overflow occurred.
709713
@warn_unused_result
710714
@_transparent
711715
public func ${x.name}WithOverflow(
712-
rhs: ${Self}
716+
${x.firstArg} rhs: ${Self}
713717
) -> (partialValue: ${Self}, overflow: ArithmeticOverflow) {
714718

715719
% if x.kind == '/':

0 commit comments

Comments
 (0)