-
Notifications
You must be signed in to change notification settings - Fork 48
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
[부나] 2단계 재화 미션 제출합니다. #26
Changes from 1 commit
a7a3ad9
b100514
79a07c9
8b7cc29
4e05fce
ffbf8c3
708d742
9ad002d
491ff6a
b2de7d3
902b7c9
4f6292c
521ed80
db6e20d
cc52965
9b4c856
c9babbe
4c4311b
d7e8da7
4054061
558c694
4b6cfed
8a05f31
15ef714
87709b2
9685128
c68c1f1
471bf9a
05e244c
3f9f6e8
f5a1fd1
53e96a7
948c74f
740390a
ea3d85a
aecf209
aea0e58
2dd17a1
f479f24
93b2b76
9a6a1cd
3e167d6
46b6475
0196ee7
8f88737
c33533c
1255597
ab5d8e8
611aca8
6211ba8
59459aa
31df852
743fb78
c716daa
8a67f4f
6637bb7
ce80e22
ccf431b
a9a1cbf
02836bc
e1fded7
23e0295
6deb885
7b5f9a4
a59cc1b
5f2fb98
f2f6a9b
4497d33
a70f99e
0bc40f5
f2bd29c
a82f66f
5c9cea7
1e09e7a
1a46c16
1a2b2ff
39f71be
d9c98c2
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 |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package woowacourse.shopping.util.collection | ||
|
||
class DistinctList<E> : ArrayList<E>() { | ||
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. 중복으로 추가되지 않도록 하는 리스트를 구현해주셨네요. List의 함수 중에서는 public fun <T> Iterable<T>.distinct(): List<T> {
return this.toMutableSet().toList()
} 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. 매번 distinct를 사용한다는 것이 불편하게 느껴져서 DistinctList 자료구조를 임의로 만들어 보았습니다! |
||
|
||
override fun addAll(elements: Collection<E>): Boolean { | ||
val temp = mutableListOf<E>() | ||
elements.forEach { element -> | ||
if (!contains(element)) { | ||
temp.add(element) | ||
} | ||
} | ||
return super.addAll(temp) | ||
} | ||
|
||
override fun add(element: E): Boolean { | ||
if (!contains(element)) { | ||
return super.add(element) | ||
} | ||
return false | ||
} | ||
} |
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.
위의 if문에서 response.body가 널이 아님을 체크했는데도 non-null assertion(!!)을 왜 사용해야하는지 궁금하지 않으신가요? 😄
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.
response.body() 메서드가 반환하는 타입을 살펴보면 원인을 알 수 있습니다.
response.body() 메서드는 Nullable한 타입을 반환하고 있습니다.
body
필드가 final이기 때문에, response.body()에 대해 null 체크를 한 번만 해주면 개발자는 이 값이 null이 아님을 알 수 있습니다.하지만 컴파일러는
response.body() 내부에서 어떤 값을 반환하는데, 이 값이 Nullable하다
라는 정보만으로 검사하기 때문에 !!을 사용해주어야 합니다.위 방법을 피하고 싶다면, 아래와 같이 코드를 작성해볼 수도 있을 것 같습니다.
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.
잘 이해하고 계시네요. 👍
이런 경우에는 scope function (apply, also, let, with, run 등)을 활용하기도 합니다.