-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Add type restrictions to Log directory #15777
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
base: master
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,7 +14,7 @@ class Log | |
|
||
# Creates an instance of a `Log::Formatter` that calls | ||
# the specified `Proc` for every entry | ||
def self.new(&proc : (Log::Entry, IO) ->) | ||
def self.new(&proc : (Log::Entry, IO) ->) : self | ||
ProcFormatter.new proc | ||
end | ||
end | ||
|
@@ -69,7 +69,7 @@ class Log | |
end | ||
|
||
# Write a fixed string | ||
def string(str) : Nil | ||
def string(str : String) : Nil | ||
straight-shoota marked this conversation as resolved.
Show resolved
Hide resolved
|
||
@io << str | ||
end | ||
|
||
|
@@ -96,7 +96,7 @@ class Log | |
# Log.setup(:info, Log::IOBackend.new(formatter: TestFormatter)) | ||
# Log.for("foo.bar").info { "Hello" } # => - [foo.bar] Hello | ||
# ``` | ||
def source(*, before = nil, after = nil) | ||
def source(*, before : _ = nil, after : _ = nil) : Nil | ||
if @entry.source.size > 0 | ||
@io << before << @entry.source << after | ||
end | ||
|
@@ -107,7 +107,7 @@ class Log | |
# It doesn't write any output if the entry data is empty. | ||
# Parameters `before` and `after` can be provided to be written around | ||
# the value. | ||
def data(*, before = nil, after = nil) : Nil | ||
def data(*, before : _ = nil, after : _ = nil) : Nil | ||
unless @entry.data.empty? | ||
@io << before << @entry.data << after | ||
end | ||
|
@@ -118,7 +118,7 @@ class Log | |
# It doesn't write any output if the context is empty. | ||
# Parameters `before` and `after` can be provided to be written around | ||
# the value. | ||
def context(*, before = nil, after = nil) | ||
def context(*, before : _ = nil, after : _ = nil) : Nil | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I believe we're on the side of preferring no type restriction instead of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If the type is really unrestricted - like in this case - I think it makes a lot of sense to explicate that in the type restriction There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That is also my preference, especially from a documentation perspective, but I know others on the team disagreed. Another alternative proposed in that issue: def context(*, before : B = nil, after : A = nil) : Nil forall A, B
end There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
unless @entry.context.empty? | ||
@io << before << @entry.context << after | ||
end | ||
|
@@ -130,7 +130,7 @@ class Log | |
# Parameters `before` and `after` can be provided to be written around | ||
# the value. `before` defaults to `'\n'` so the exception is written | ||
# on a separate line | ||
def exception(*, before = '\n', after = nil) : Nil | ||
def exception(*, before : _ = '\n', after : _ = nil) : Nil | ||
if ex = @entry.exception | ||
@io << before | ||
ex.inspect_with_backtrace(@io) | ||
|
@@ -149,7 +149,7 @@ class Log | |
end | ||
|
||
# Write the `Log::Entry` to the `IO` using this pattern | ||
def self.format(entry, io) : Nil | ||
def self.format(entry : Log::Entry, io : IO) : Nil | ||
new(entry, io).run | ||
end | ||
|
||
|
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.
.new
methods are always returningself
, making return type annotation IMO pretty redundant.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.
For documentation purpose, yes, but there are cases where the compiler doesn't always infer that a specific
.new
returns aself
.