-
Notifications
You must be signed in to change notification settings - Fork 27
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
Convert mutable type which is unhasable to immutable types #123
Convert mutable type which is unhasable to immutable types #123
Conversation
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.
함수 이름에 대한 댓글을 많이 달았습니다. toX
와 같은 이름들이 쓰였는데, 하스켈의 X
로 만든다는 느낌이 먼저 들었기 때문입니다. 실제로는 파이썬의 값을 파이썬의 X
로 만드는 파이썬 코드를 생성해주는 함수였기 때문에 다른 이름들을 제안했습니다.
src/Nirum/Targets/Python.hs
Outdated
attributeValue :: T.Text | ||
attributeValue = case fieldType' of | ||
SetModifier _ -> [qq|frozenset($attributeName)|] | ||
ListModifier _ -> [qq|tuple($attributeName)|] |
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.
리스트를 튜플로 표현하는 것이 적절하지는 않은 것 같습니다. 튜플을 불변 리스트로 보는 사람이 별로 없기 때문입니다. 차라리 런타임에 ImmutableList
를 만들어서 넣고 그걸 쓰는 건 어떨까요?
src/Nirum/Targets/Python.hs
Outdated
fieldName' :: Name | ||
fieldName' = fieldName field | ||
fieldType' :: TypeExpression | ||
fieldType' = TD.fieldType field |
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.
fieldName'
, fieldType'
이 둘은 getter 쓰는 것보다 데이터 생성자 써서 패턴 매칭으로 구하는 게 더 깔끔하지 않을까요?
src/Nirum/Targets/Python.hs
Outdated
@@ -329,6 +339,28 @@ returnCompiler = do | |||
Python2 -> "" | |||
Python3 -> [qq| -> $r|] | |||
|
|||
convertFieldToImmutable :: Field -> Code | |||
convertFieldToImmutable field = |
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.
compileFieldInitializer
정도의 이름을 제안합니다.
src/Nirum/Targets/Python.hs
Outdated
_ -> attributeName | ||
|
||
toClassInitialValues :: [Field] -> [Code] | ||
toClassInitialValues fields = |
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.
비슷하게 compileClassFieldInitializers
정도의 이름을 제안합니다.
src/Nirum/Targets/Python.hs
Outdated
toArgumentCode :: (ParameterName -> ParameterType -> Code) | ||
-> [(T.Text, Code)] | ||
-> Code | ||
toArgumentCode gen nameNTypes = toIndentedCodes (uncurry gen) nameNTypes ", " |
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.
Parameter는 보통 형식 인자(인자의 이름이나 타입과 같은 모양)을 뜻하고 argument는 그 인자에 들어가는 실제 인자값을 뜻합니다. 예를 들어 def f(a, b):
같은 함수를 정의할 때 a
나 b
는 parameters지만 그걸 호출하는 f(1, 2)
에서 1
이나 2
는 arguments입니다.
그런 차원에서 생각하면 함수 이름은 argument라는 말보다는 parameter라는 말을 쓰는 게 나을 것 같네요.
compileParameters
정도의 이름을 제안합니다.
src/Nirum/Targets/Python.hs
Outdated
toArgumentCode gen nameNTypes = toIndentedCodes (uncurry gen) nameNTypes ", " | ||
|
||
toInitialValueCode :: DS.DeclarationSet Field -> Code | ||
toInitialValueCode fields = |
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.
compileFieldInitializers
정도의 이름을 제안합니다. 그리고 이하의 convertFieldToImmutable
, toClassInitialValues
같은 함수들은 단위 테스트를 붙이기 위한 것이 아니라면 차라리 이 함수의 where
절 아래로 넣는 게 낫지 않을까요?
@dahlia |
src/Nirum/Targets/Python.hs
Outdated
case fieldType' of | ||
SetModifier _ -> return [qq|frozenset($attributeName)|] | ||
ListModifier _ -> do | ||
let imports = [("nirum.datastructures", ["List"])] |
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.
It prevents Nirum types to be named as list
since it can make name conflict. Seems nirum.datastructures
need to export aliases e.g. list_type
, map_type
, etc.
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.
i need to modify nirum-python first, then i'll fix this!
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.
nirum-lang/nirum-python#70 nirum-python fixed. From now on, List
aliased as list_type
.
5d61368
to
7ad23b8
Compare
🙇🏼 Kore o kakuninshitekudasai 🙇🏼 |
test/python/typeconversion_test.py
Outdated
album = Album(name='25', tracks=[ | ||
Song(name='Hello') | ||
]) | ||
assert isinstance(album.tracks, tuple) |
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.
tuple
이 아니라 nirum.datastructures.List
인지 검사해야 할 것 같습니다.
- Gather some functions in where - Rename convertFieldToImmutable -> compileFieldInitializer - Use pattern-matching to get value of field
7ad23b8
to
f7143e6
Compare
- Update minimum required version of nirum - Unicode lietral have to be used on Python2.7
Importing "List" would raise error, because "List" could be named on user-define variable. so list_type which is alias for List is imported.
it fixes nirum-lang/nirum-python#49. this PR propose convert a type that could be mutable to immutable type always rather than raise error on runtime.