-
Notifications
You must be signed in to change notification settings - Fork 1.1k
[DRAFT] Allow customisation of metaprogramming via scalac flags #12039
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
compiler/src/dotty/tools/dotc/core/CompileTimeEnvMap.scala
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package dotty.tools.dotc.core | ||
|
||
import dotty.tools.dotc.config.ScalaSettings | ||
import dotty.tools.dotc.config.Settings.Setting.value | ||
import Contexts._ | ||
|
||
// TODO doc | ||
final case class CompileTimeEnvMap(env: Map[String, String]) { | ||
def apply(key: String): Option[String] = | ||
env.get(key) | ||
} | ||
|
||
object CompileTimeEnvMap { | ||
|
||
def fromSettings(using Context): CompileTimeEnvMap = { | ||
var m = Map.empty[String, String] | ||
|
||
for (s <- ctx.settings.compileTimeEnv.value) | ||
val i = s.indexOf('=') | ||
if i > 0 then | ||
// -Ekey=value | ||
val key = s.take(i) | ||
val value = s.drop(i + 1) | ||
m = m.updated(key, value) | ||
else if i < 0 then | ||
// -Ekey | ||
val key = s | ||
if !m.contains(key) then m = m.updated(key, "") | ||
|
||
new CompileTimeEnvMap(m) | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
a = [] | ||
b = [1] | ||
c.b.a = [x.y.z=1] | ||
wat is not defined |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import scala.compiletime.* | ||
|
||
object Test { | ||
|
||
inline def logEnv(inline k: String): Unit = | ||
inline envGet(k) match | ||
case Some(v) => println(s"$k = [$v]") | ||
case None => println(k + " is not defined") | ||
|
||
def main(args: Array[String]): Unit = { | ||
logEnv("a") | ||
logEnv("b") | ||
logEnv("c.b.a") | ||
logEnv("wat") | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
I'm a info msg | ||
I'm a warn msg |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import scala.compiletime.* | ||
|
||
object Logging { | ||
|
||
// Just use your imagination for now :) | ||
private inline val Trace = 0 | ||
private inline val Debug = 1 | ||
private inline val Info = 2 | ||
private inline val Warn = 3 | ||
|
||
private transparent inline def chosenThreshold: Int = | ||
inline envGet("myLogger.level") match | ||
case Some("TRACE") => Trace | ||
case Some("DEBUG") => Debug | ||
case Some("INFO") => Info | ||
case Some("WARN") => Warn | ||
case Some(x) => error("Unsupported logging level: " + x) | ||
case None => Trace | ||
|
||
private inline def log(inline lvl: Int, inline msg: String): Unit = | ||
inline if lvl >= chosenThreshold then println(msg) else () | ||
|
||
inline def trace(inline msg: String) = log(Trace, msg) | ||
inline def debug(inline msg: String) = log(Debug, msg) | ||
inline def info (inline msg: String) = log(Info , msg) | ||
inline def warn (inline msg: String) = log(Warn , msg) | ||
} | ||
|
||
object Test { | ||
import Logging.* | ||
|
||
def main(args: Array[String]): Unit = { | ||
trace("I'm a trace msg") | ||
debug("I'm a debug msg") | ||
info("I'm a info msg") | ||
warn("I'm a warn msg") | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This kind of logic should be in the reflection interface first. If it is, this method can be implemented in any external library.