romstad / Chess.jl

Julia chess programming library.
https://romstad.github.io/Chess.jl/dev/
Other
105 stars 18 forks source link

Relax `::String` parameters to `::AbstractString` #35

Open mcognetta opened 2 years ago

mcognetta commented 2 years ago

This relaxes every function which takes a String parameter to accept ::AbstractString instead. Importantly, all constructors with string fields are left alone, which is fine because constructors convert types to the expected type during initialization.

This PR is intended to help with things like reading games from files or from data streams where strings are not always materialized (they may be bytes and just acting like string [a la. https://github.com/JuliaStrings/StringViews.jl], views, substrings, etc). In the end, many of these will end up getting converted to strings, but it lifts the burden from the caller to ensure that everything is converted to a string before the callsite.

One concrete usecase is when reading uci strings:

julia> g = SimpleGame()
SimpleGame:
 *

julia> moves = "d2d4 g8f6"
"d2d4 g8f6"

julia> for m in split(moves, " ")
           domove!(g, m)
       end

julia> g
SimpleGame:
 1. d4 Nf6 *

However, split(moves, " ") is not a Vector{String}:

julia> split(moves, " ")
2-element Vector{SubString{String}}:
 "d2d4"
 "g8f6"

and so domove! fails on master.