Skip to content
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

implemented move_alloc for string_type #467

Merged
merged 7 commits into from
Aug 26, 2021

Conversation

aman-godara
Copy link
Member

@aman-godara aman-godara commented Jul 16, 2021

Closes #449

Tasks:

  • Implemented move
  • added test cases for move
  • documented newly created function
  • Pure vs Elemental?
  • Name of the function?: as of now it is named as move
  • move if both arguments are of type character(len=:)?: move_alloc does the same thing OR should we consider renaming it from move to move_alloc.

@awvwgk
Copy link
Member

awvwgk commented Jul 16, 2021

Pure vs Elemental?

It can't be elemental since it might handle an allocatable data type in case of deferred length characters. You could swap two string_type arrays with an elemental subroutine, though. Not sure if this is a common application of this procedure.

move if both arguments are of type character(len=:)?: move_alloc does the same thing OR should we consider renaming it from move to move_alloc.

It can't overload move_alloc since we won't be able to use the procedure in the context of type(string_type), allocatable anymore, like in the example below:

use stdlib_string_type
type(string_type), allocatable :: str1, str2
str1 = string_type("Moveable string value")
call move_alloc(str1, str2)
end

Copy link
Member

@awvwgk awvwgk left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a useful addition, thanks.

@aman-godara
Copy link
Member Author

Requesting Clarification:

As I understand, the function can't be made elemental because a user cannot create an array in which each element is of type character(len=:), allocatable? Have I understood it correctly?

@aman-godara
Copy link
Member Author

aman-godara commented Jul 16, 2021

How about instead of overloading the move_alloc we provide the functionality of move_alloc (for character allocatable) in move as well. To do this we will just call move_alloc inside move and that will do the job.

character(len=:), allocatable :: char_1, char_2
char_1 = "moveable char"
move(char_1, char_2)         ! char_2 <-- "moveable char"
move_alloc(char_2, char_1)   ! char_1 <-- "moveable char"

above expected behaviour can be carried out using move_alloc as well. Thus, both move_alloc and move can do this.

@aman-godara aman-godara mentioned this pull request Jul 21, 2021
10 tasks
@aman-godara
Copy link
Member Author

@awvwgk I have added a commit 73ec8c5 corresponding to comment mentioned just above by me.

I added move for both arguments as character allocatable (character(len=:), allocatable) so that a user need not to use a boilerplate if-else condition before using move to use move_alloc if both arguments are character allocatable.
If there are no repercussions, then in my opinion this will be good to have. Let me know your thoughts on this.

Comment on lines +738 to +741
type(string_type), intent(inout) :: from
type(string_type), intent(out) :: to

call move_alloc(from%raw, to%raw)
Copy link
Contributor

@zoziha zoziha Aug 8, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

move_alloc requires its arguments to be allocatable.
The move function arguments here are type(string_type), which is not allocatable.
If we overload move_alloc, it may cause users to misunderstand that move_alloc is different from the Fortran standard at allocatable attribute .
From this perspective, I think the name can be move or move_alloc_, better not move_alloc?

But from the perspective of actual use and as an extended function, I think move_alloc is also good.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I found move to be intuitive as well. As if it cut and pastes (not exactly) a string from one location to other. Similar to moving a file from one folder to other.

Copy link
Member

@milancurcic milancurcic left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very nice, thanks @aman-godara. We'll merge it tomorrow if there are no objections.

@aman-godara
Copy link
Member Author

But wait! I have objections here. Have a look at commit 73ec8c5 once. I will also improve test cases and documentation for this.

@aman-godara
Copy link
Member Author

aman-godara commented Aug 25, 2021

Requesting Clarification:

As I understand, the function can't be made elemental because a user cannot create an array in which each element is of type character(len=:), allocatable? Have I understood it correctly?

Did I understand it correctly?


I have made all the change that I wanted to make. PR is open to be merged from my side. I found the behaviour of the last test case non-intuitive but I think we cannot do anything with it.

@milancurcic
Copy link
Member

@aman-godara correct, the dummy argument can't be allocatable in an elemental procedure.

I also don't see a use case for the last test, but I can't tell that it hurts to leave it there.

Let's merge tomorrow if no objections. Thanks again!

@milancurcic milancurcic merged commit eae0027 into fortran-lang:master Aug 26, 2021
@awvwgk awvwgk removed the reviewers needed This patch requires extra eyes label Sep 25, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

move_alloc for strings
4 participants