@@ -2,8 +2,8 @@ SourceCode [
21
21
```
22
22
23
23
This might not be something you want to use for "business logic", but is very
24
- helpful for things like [ debugging] ( #debug-prints ) , [ logging] ( #logging ) or
25
- providing automatic diagnostics for [ DSLs] ( #embedding-domain-specific-languages ) .
24
+ helpful for things like [ debugging] ( #debug-prints ) , [ logging] ( #logging ) or
25
+ providing automatic diagnostics for [ DSLs] ( #embedding-domain-specific-languages ) .
26
26
This information is also available via an ` implicit ` , letting you write functions
27
- that automatically pull it in.
27
+ that automatically pull it in.
28
28
29
29
Using SourceCode on code dealing with lots of anonymous functions or anonymous
30
30
classes can easily turn what you see in your debug printouts from this:
@@ -35,10 +35,10 @@ To this:
35
35
36
36
![ After] ( docs/After.png )
37
37
38
- By capturing source information you can use to give your objects and function
39
- meaningful names that tell you where they were defined, automatically without
40
- needing you to manually assign a string-ID to every anonymous function or
41
- anonymous class you define all over your code bas.
38
+ By capturing source information you can use to give your objects and function
39
+ meaningful names that tell you where they were defined, automatically without
40
+ needing you to manually assign a string-ID to every anonymous function or
41
+ anonymous class you define all over your code bas.
42
42
43
43
If you like using Sourcecode, you might also enjoy this book by the author which
44
44
teaches you Scala in a similarly simple and straightforward way:
@@ -74,14 +74,14 @@ The kinds of compilation-time data that `sourcecode` provides are:
74
74
- ` sourcecode.Enclosing ` : the name of the nearest enclosing definition: ` val ` ,
75
75
` class ` , whatever, prefixed by the names of all enclosing ` class ` s, ` trait ` s,
76
76
` object ` s or ` package ` s, ` def ` s, ` val ` s, ` var ` s or ` lazy val ` s`
77
- - ` sourcecode.Text[T] ` : when you want to take a value of type ` T ` , but also
78
- want to get the "source text" of that particular value. Note that if
79
- you have multiple statements in a ` {} ` block, ` sourcecode.Text ` will only
77
+ - ` sourcecode.Text[T] ` : when you want to take a value of type ` T ` , but also
78
+ want to get the "source text" of that particular value. Note that if
79
+ you have multiple statements in a ` {} ` block, ` sourcecode.Text ` will only
80
80
capture the source code for the last expression that gets returned. This
81
81
implicit is slightly experimental; be sure to report any bugs you find!
82
- - ` sourcecode.Args ` : the arguments that where provided to the nearest enclosing
82
+ - ` sourcecode.Args ` : the arguments that where provided to the nearest enclosing
83
83
method
84
- - ` sourcecode.Name.Machine ` , ` sourcecode.FullName.Machine ` and
84
+ - ` sourcecode.Name.Machine ` , ` sourcecode.FullName.Machine ` and
85
85
` sourcecode.Enclosing.Machine ` which are similar to ` sourcecode.Name ` ,
86
86
` sourcecode.FullName ` and ` sourcecode.Enclosing ` except they do not filter
87
87
out synthetic method names; e.g. if you want to see the ` <init> ` names or
@@ -110,7 +110,7 @@ can't be used.
110
110
Examples
111
111
========
112
112
113
- Here are a few examples of ` sourcecode ` 's core functions being used in a
113
+ Here are a few examples of ` sourcecode ` 's core functions being used in a
114
114
variety of contexts. Hopefully they will give you an idea of how the various
115
115
implicits behave:
116
116
@@ -170,8 +170,8 @@ object Implicits {
170
170
}
171
171
```
172
172
173
- Note that in "normal" usage you would not directly call ` implicitly ` to summon
174
- up ` sourcecode ` values; rather, you would add implicit parameters of these
173
+ Note that in "normal" usage you would not directly call ` implicitly ` to summon
174
+ up ` sourcecode ` values; rather, you would add implicit parameters of these
175
175
types to your functions. That would make these values automatically available
176
176
to your functions without needing to manually keep passing them in. Apart from
177
177
summoning them via implicits, you can also use the ` apply ` method on each type
@@ -233,7 +233,7 @@ object Implicits {
233
233
}
234
234
```
235
235
236
- By default, the various implicits all ignore any synthetic ` <init> ` ,
236
+ By default, the various implicits all ignore any synthetic ` <init> ` ,
237
237
` <local Foo> ` or ` $anonfun ` methods that might be present:
238
238
239
239
``` scala
@@ -284,10 +284,10 @@ object Synthetic {
284
284
```
285
285
286
286
Hopefully this has given you a reasonable feel for * what** sourcecode does. You
287
- may still be wondering * why* we would want any of this: what could we possibly
288
- use these things for? Why would we want to write code that depends on our
287
+ may still be wondering * why* we would want any of this: what could we possibly
288
+ use these things for? Why would we want to write code that depends on our
289
289
package paths or variable names? The section below will provide use cases that
290
- you will hopefully be able to relate to.
290
+ you will hopefully be able to relate to.
291
291
292
292
Use Cases
293
293
=========
@@ -318,7 +318,7 @@ This can be handy for letting you see where the log lines are coming from,
318
318
without tediously tagging every log statement with a unique prefix.
319
319
Furthermore, this happens at compile time, and is thus orders of magnitude
320
320
faster than getting this information by generating stack traces, and works
321
- on Scala.js where stack-inspection does not. Lastly, if you want additional
321
+ on Scala.js where stack-inspection does not. Lastly, if you want additional
322
322
information such as method names, class names, or packages to be provided to
323
323
your logging function, you can easily do so by asking for the ` sourcecode.Name `
324
324
or ` sourcecode.FullName ` or ` sourcecode.Pkg ` implicits.
@@ -362,7 +362,7 @@ named enums (or even an enum of the same name in a different package!) are
362
362
given unique names. In that case, you can use ` sourcecode.FullName ` or
363
363
` sourcecode.Enclosing ` to capture the full path e.g.
364
364
` "com.mypkg.MyEnum.firstItem" ` and ` "com.mypkg.MyEnum.secondItem" ` :
365
-
365
+
366
366
``` scala
367
367
package sourcecode
368
368
@@ -383,7 +383,7 @@ object EnumFull {
383
383
}
384
384
}
385
385
```
386
- You can also use ` sourcecode.Name ` in an constructor, in which case it'll be
386
+ You can also use ` sourcecode.Name ` in an constructor, in which case it'll be
387
387
picked up during inheritance:
388
388
389
389
``` scala
@@ -425,7 +425,7 @@ class Foo(arg: Int){
425
425
new Foo (123 ).bar(" lol" ) // sourcecode.DebugRun.main Foo#bar [arg -> param]: (123,lol)
426
426
```
427
427
428
- You can easily vary the amount of verbosity, e.g. by swapping the
428
+ You can easily vary the amount of verbosity, e.g. by swapping the
429
429
` sourcecode.Enclosing ` for a ` sourcecode.Name ` if you think it's too verbose:
430
430
431
431
``` scala
@@ -460,11 +460,11 @@ new Foo(123).bar("lol") // [param]: lol
460
460
461
461
Thus you can easily configure how much information your ` debug ` helper method
462
462
needs, at its definition, without having to hunt all over your codebase for the
463
- various ` debug ` call-sites you left lying around and manually tweaking the
463
+ various ` debug ` call-sites you left lying around and manually tweaking the
464
464
verbosity of each one. Furthermore, if you want additional information like
465
465
` sourcecode.Line ` or ` sourcecode.File ` , that's all just one implicit away.
466
466
467
- The [ PPrint] ( http://www.lihaoyi.com/upickle-pprint/pprint )
467
+ The [ PPrint] ( http://www.lihaoyi.com/upickle-pprint/pprint )
468
468
library provides a ` pprint.log ` method that does exactly this: prints out the
469
469
value provided (in this case pretty-printing it with colors and nice formatting
470
470
& indentation) together with the enclosing context and line number, so you
@@ -473,8 +473,8 @@ can easily distinguish your individual prints later:
473
473
``` scala
474
474
scala> class Foo {
475
475
| def bar (grid : Seq [Seq [Int ]]) = {
476
- | // automatically capture and print out source context
477
- | pprint.log(grid, tag= " grid" )
476
+ | // automatically capture and print out source context
477
+ | pprint.log(grid, tag= " grid" )
478
478
| }
479
479
| }
480
480
defined class Foo
@@ -488,7 +488,7 @@ List(
488
488
)
489
489
```
490
490
491
- ` pprint.log ` is itself defined as
491
+ ` pprint.log ` is itself defined as
492
492
493
493
``` scala
494
494
def log [T : PPrint ](value : T , tag : String = " " )
@@ -499,10 +499,10 @@ def log[T: PPrint](value: T, tag: String = "")
499
499
500
500
Using ` sourcecode.Enclosing ` and ` sourcecode.Line ` to provide the context to
501
501
be printed. You can, or course, define your own ` log ` method in the same way,
502
- customizing it to print or not-print exactly what you want to see via the
502
+ customizing it to print or not-print exactly what you want to see via the
503
503
implicits that ` sourcecode ` provides!
504
504
505
- ` sourcecode.Args ` can be used to access all parameters that where provided
505
+ ` sourcecode.Args ` can be used to access all parameters that where provided
506
506
to a method:
507
507
508
508
``` scala
@@ -521,7 +521,7 @@ Embedding Domain-Specific Languages
521
521
-----------------------------------
522
522
523
523
The Scala programming is a popular choice to embed domain-specific languages:
524
- that means that you start with some external language, e.g. this
524
+ that means that you start with some external language, e.g. this
525
525
[ MathProg] example
526
526
527
527
``` scala
@@ -542,7 +542,7 @@ var y{K} >= 0;
542
542
```
543
543
544
544
The linked slides has more detail about what exactly this language does (it
545
- describes mathematical optimization problems). For a variety of reasons, you
545
+ describes mathematical optimization problems). For a variety of reasons, you
546
546
may prefer to write this as part of a Scala program instead: for example you
547
547
may want Scala's IDE support, or its ability to define functions that help
548
548
reduce boilerplate, or maybe you like the way the compiler provides type errors
@@ -574,8 +574,8 @@ when printing error messages, or the results of the computation, you want to
574
574
see which ` val ` s are involved! Thus you end up duplicating the names over and
575
575
over and over.
576
576
577
- With sourcecode, you can easily define ` param ` ` set ` and ` xvar ` as taking
578
- implicit ` sourcecode.Name ` s, thus eliminating all the boilerplate involved in
577
+ With sourcecode, you can easily define ` param ` ` set ` and ` xvar ` as taking
578
+ implicit ` sourcecode.Name ` s, thus eliminating all the boilerplate involved in
579
579
duplicating names:
580
580
581
581
``` scala
@@ -609,8 +609,8 @@ C.parse("X") // Failure((A | B):1:1 ..."X")
609
609
```
610
610
611
611
As you can see, the names of the rules ` A ` and ` B ` are embedded in the error
612
- messages for parse failures. This makes debugging parsers far easier, while
613
- saving you the effort of duplicating the name of the parser in possibly
612
+ messages for parse failures. This makes debugging parsers far easier, while
613
+ saving you the effort of duplicating the name of the parser in possibly
614
614
hundreds of rules in a large parser. In this case, it is the ` P(...) ` function
615
615
which takes an implicit ` sourcecode.Name ` that does this work:
616
616
@@ -625,6 +625,11 @@ in its `.toString` method.
625
625
Version History
626
626
===============
627
627
628
+ 0.2.5
629
+ -----
630
+
631
+ - Support Scala 3.0.0-RC2
632
+
628
633
0.2.2
629
634
-----
630
635
@@ -659,41 +664,41 @@ Version History
659
664
0.1.3
660
665
-----
661
666
662
- - Add scala 2.12.x support, thanks to
667
+ - Add scala 2.12.x support, thanks to
663
668
[ Lars Hupel] ( https://github.com/larsrh )
664
669
665
670
0.1.2
666
671
-----
667
672
668
673
- Add ` sourcecode.Args ` implicit, which can be used to capture debugging information
669
- about the nearest enclosing function call for logging/debugging, thanks to
674
+ about the nearest enclosing function call for logging/debugging, thanks to
670
675
[ Benjamin Hagemeister] ( https://github.com/benhag )
671
676
672
- - Attempted fix for [ #17 ] ( https://github.com/lihaoyi/sourcecode/issues/17 ) and
677
+ - Attempted fix for [ #17 ] ( https://github.com/lihaoyi/sourcecode/issues/17 ) and
673
678
[ #13 ] ( https://github.com/lihaoyi/sourcecode/issues/13 ) , thanks to
674
- [ Simeon H.K. Fitch] ( https://github.com/metasim )
679
+ [ Simeon H.K. Fitch] ( https://github.com/metasim )
675
680
676
681
0.1.1
677
682
-----
678
683
679
- - Ignore ` <local foo> ` and ` <init> ` symbols when determining ` sourcecode.Name ` ,
684
+ - Ignore ` <local foo> ` and ` <init> ` symbols when determining ` sourcecode.Name ` ,
680
685
` sourcecode.FullName ` or ` sourcecode.Enclosing ` . If you want these, use the
681
686
` sourcecode.Name.Machine ` /` sourcecode.FullName.Machine ` /` sourcecode.Enclosing.Machine `
682
687
implicits instead.
683
-
688
+
684
689
- Add ` sourcecode.Text ` implicit to capture source code of an expression
685
690
686
691
- Add implicit conversions to ` sourcecode.* ` , so you can pass in a ` String `
687
- to manually satisfy and implicit wanting a ` sourcecode.Name ` or
688
- ` sourcecode.FullName ` or ` sourcecode.File ` , an ` Int ` to satisfy an implicit
689
- asking for ` sourcecode.Line `
692
+ to manually satisfy and implicit wanting a ` sourcecode.Name ` or
693
+ ` sourcecode.FullName ` or ` sourcecode.File ` , an ` Int ` to satisfy an implicit
694
+ asking for ` sourcecode.Line `
690
695
691
696
- ` sourcecode.Enclosing ` has been simplified to take a single ` String ` rather
692
697
than the previous ` Vector[Chunk] ` .
693
-
694
- - Added the ` sourcecode.Pkg ` implicit, which provides the current
695
- enclosing package without any of the ` class ` s/` object ` s/` def ` s/etc.. Can be
696
- subtracted from ` sourcecode.Enclosing ` if you * only* want the
698
+
699
+ - Added the ` sourcecode.Pkg ` implicit, which provides the current
700
+ enclosing package without any of the ` class ` s/` object ` s/` def ` s/etc.. Can be
701
+ subtracted from ` sourcecode.Enclosing ` if you * only* want the
697
702
` class ` s/` object ` s/` def ` s/etc.
698
703
699
704
0.1.0
0 commit comments