-
Notifications
You must be signed in to change notification settings - Fork 38
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
Pattern matching with substrings #62
Comments
Hello! @match y begin
[&"a", z, &"c"] => :okay
_ => :nomatch
end as a workaround. You might feel like to check Reference Pattern in MLStyle.jl, aka Pin operator in Elixir. |
When I use Julia 1.0.* with MLStyle.jl v And Julia 1.1.* do have this issue. |
@inkydragon Say, we can have an arbitrary abstract type AbstractMySpecificString end
eq = :(==)
@eval Base.$eq(::AbstractMySpecificString, ::Any) = false
@match AbstractMySpecificString() begin
1 => true
_ => false
end What should the output be? Initially I feel like to support some of the |
@inkydragon This behaviour is actually been aware of: https://github.com/thautwarm/MLStyle.jl/blob/master/src/Pervasives.jl#L19 However I also think matching SubString with String literals makes sense. |
I also expected that SubStrings would match with string literals. |
@oxinabox Understood. It's a problem, but I don't feel like to support matching SubStrings here unless we have solved the issue presented in #62 (comment) If we support matching SubStrings and Strings with String literals directly, it'll raise consistency problems. |
support anything that is Re: #62 (comment)
Why would the output not be |
After pondering for a while, I think there're still some issues. I hope people could help me with following issues, because Julia programmers' mental model is more crucial than the technical parts. Issues
In fact, other canonical languages with pattern matching like Haskell, OCaml and F# won't let you match case 1 of 1.0 -> 1 match 1 with 1.0 -> 1 They all failed when compiling, or produced a warning in REPL. If we allow @match 1 begin 1.0 => 1 end in Julia according to However, Scala supports
A constructor might accept a single argument typed For instance, pattern matching is super useful in compiler stuffs. We could use ANF via MLStyle's @data Exp{T} begin
Value(T)
...
end Now, the intermediate representation of integer function mytransformation(ir::Exp{T}) where T
@match ir begin
Value(1) => ...
Value(1.0) => ...
end
end ProposalsWe can keep literal patterns as simple as possible, and make We can use the reference pattern/pin operator |
In my mental model I naturally assumed pattern matching would use x = 10
@match x begin
10::Float64 => :float
10::Int => :int
_ => :no_match
end I guess you could argue this is more natural since it relies on the type syntax Julia users should already be familiar with, rather than having to introduce additional syntax such as This also aligns with how matching seems to work on a Dict currently, it doesn't care if the key is a SubString or a String. d = Dict( k => i for (i , k) in enumerate(split("a b c")))
@match d begin
Dict("a" => a ) => :substring_key
_ => :no_match
end
Side note - following this logic I'd expect the following to work but it currently doesn't. @match d begin
Dict{SubString,Int}("a" => a ) => :match
_ => :no_match
end
#LoadError: PatternUnsolvedException Either way I think as long as the documentation mentions if pattern matching is based on |
Hi, some of the Julia 1.1+ versions broke the String equality by I'm now planning to use julia> using BenchmarkTools
julia> a = "123"
"123"
julia> b = "123"
"123"
julia> c = "234"
"234"
julia> @btime a == b
12.796 ns (0 allocations: 0 bytes)
true
julia> @btime a === b
6.091 ns (0 allocations: 0 bytes)
true
julia> @btime a == c
12.992 ns (0 allocations: 0 bytes)
false
julia> @btime a === c
6.088 ns (0 allocations: 0 bytes) Is performance loss affordable for you? |
First off thanks for the package it works great. I'm not sure if this is and bug or the intended semantics (if so feel free to close) but when pattern matching on a
String
type the following works fineBut the same pattern fails on a
SubString
The text was updated successfully, but these errors were encountered: