Skip to content

Commit 8bb74d7

Browse files
Add InlineNumeric prototype skeleton
1 parent f28d708 commit 8bb74d7

File tree

1 file changed

+50
-0
lines changed

1 file changed

+50
-0
lines changed

InlineNumeric.scala

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
// community-build/community-projects/stdLib213/src/library/scala/math/Numeric.scala
2+
// community-build/community-projects/stdLib213/src/library/scala/math/Integral.scala
3+
// community-build/community-projects/stdLib213/src/library/scala/math/Fractional.scala
4+
5+
6+
trait InlineNumeric[T]: // extends Numeric[T] // TODO can we do this?
7+
transparent inline def plus(inline x: T, inline y: T): T
8+
transparent inline def times(inline x: T, inline y: T): T
9+
// TODO add missing methods
10+
object InlineNumeric:
11+
extension [T](inline x: T)(using inline num: InlineNumeric[T])
12+
transparent inline def +(inline y: T): T = num.plus(x, y)
13+
transparent inline def *(inline y: T): T = num.times(x, y)
14+
// TODO add missing methods
15+
16+
trait InlineIntegral[T] extends InlineNumeric[T]:
17+
transparent inline def quot(inline x: T, inline y: T): T
18+
// TODO add missing methods
19+
object InlineIntegral:
20+
// TODO: how are these imported/composed with Numeric/Integral. Should the extension methods be defined in trait InlineIntegral?
21+
extension [T](inline lhs: T)(using inline int: InlineIntegral[T])
22+
transparent inline def /(inline rhs: T) = int.quot(lhs, rhs)
23+
// TODO add missing methods
24+
25+
// TODO: InlineFractional
26+
27+
given IntIsInlineIntegral: InlineIntegral[Int] with
28+
transparent inline def plus(inline x: Int, inline y: Int): Int = x + y
29+
transparent inline def times(inline x: Int, inline y: Int): Int = x * y
30+
transparent inline def quot(inline x: Int, inline y: Int): Int = x / y
31+
// TODO add missing methods
32+
33+
given ShortIsInlineIntegral: InlineIntegral[Short] with
34+
transparent inline def plus(inline x: Short, inline y: Short): Short = (x + y).toShort
35+
transparent inline def times(inline x: Short, inline y: Short): Short = (x * y).toShort
36+
transparent inline def quot(inline x: Short, inline y: Short): Short = (x / y).toShort
37+
// TODO add missing methods
38+
39+
// TODO add missing primitive types
40+
41+
object tests:
42+
import InlineNumeric.*
43+
44+
// A generic inline operation that inlines/specializes primitive operations
45+
inline def foo[T: InlineNumeric](a: T, b: T) =
46+
a + b * b
47+
48+
def test(a: Int, b: Int) =
49+
foo(a, b) // should be a + b * b // can check with -Xprint:inlining
50+
foo(a.toShort, b.toShort) // should be a + b * b

0 commit comments

Comments
 (0)