Skip to content

Commit

Permalink
Make operators aliases.
Browse files Browse the repository at this point in the history
  • Loading branch information
tarao committed Nov 6, 2023
1 parent 44ac279 commit eadfeb1
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,10 @@ object ArrayRecord extends ArrayRecord.Extensible[EmptyTuple] {
* @return
* an object to define new fields
*/
def + : Extensible[R] = new Extensible.Appender(record)
def updated: Extensible[R] = new Extensible.Appender(record)

/** Alias for `updated` */
inline def + : Extensible[R] = updated

/** Concatenate this record and another record.
*
Expand All @@ -218,19 +221,19 @@ object ArrayRecord extends ArrayRecord.Extensible[EmptyTuple] {
* @return
* a new record which has the both fields from this record and `other`
*/
inline def ++[R2: RecordLike, RR <: ProductRecord](
inline def concat[R2: RecordLike, RR <: ProductRecord](
other: R2,
)(using concat: Concat[R, R2]): concat.Out =
)(using c: Concat[R, R2]): c.Out =
withPotentialTypingError {
val vec = record
.__fields
.toVector
.concat(summon[RecordLike[R2]].orderedIterableOf(other))
inline erasedValue[concat.NeedDedup] match {
inline erasedValue[c.NeedDedup] match {
case _: false =>
newArrayRecord[concat.Out](vec)
newArrayRecord[c.Out](vec)
case _ =>
newArrayRecord[concat.Out](
newArrayRecord[c.Out](
vec
.deduped
.iterator
Expand All @@ -239,6 +242,11 @@ object ArrayRecord extends ArrayRecord.Extensible[EmptyTuple] {
}
}

/** Alias for `concat` */
inline def ++[R2: RecordLike, RR <: ProductRecord](
other: R2,
)(using c: Concat[R, R2]): c.Out = concat(other)

/** Give a type tag to this record.
*
* @example
Expand Down
12 changes: 10 additions & 2 deletions modules/core/src/main/scala/com/github/tarao/record4s/Record.scala
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,10 @@ object Record {
* @return
* an object to define new fields
*/
def + : Extensible[R] = new Extensible(record)
def updated: Extensible[R] = new Extensible(record)

/** Alias for `updated` */
inline def + : Extensible[R] = updated

/** Concatenate this record and another record.
*
Expand All @@ -107,7 +110,7 @@ object Record {
* @return
* a new record which has the both fields from this record and `other`
*/
inline def ++[R2: RecordLike, RR <: %](
inline def concat[R2: RecordLike, RR <: %](
other: R2,
)(using Concat.Aux[R, R2, RR]): RR = withPotentialTypingError {
newMapRecord[RR](
Expand All @@ -118,6 +121,11 @@ object Record {
)
}

/** Alias for `concat` */
inline def ++[R2: RecordLike, RR <: %](
other: R2,
)(using Concat.Aux[R, R2, RR]): RR = concat(other)

/** Create a new record by selecting some fields of an existing record.
*
* @example
Expand Down

0 comments on commit eadfeb1

Please sign in to comment.