Comments (7)
If necessary I can also include the output C files
from carp.
At the moment, pattern matching is only supported for sumtypes, which are defined as follows:
(deftype Foo (Bad [Int]) (Baz [Int]))
Then, you perform a match as follows:
(defn matcher [x]
(match x)
(Bar y) y
(Baz y) y)
In your example, you've defined a product type (just a regular struct). Since product types only have one possible constructor, there's no way to match
on them. Once match supports matching on arbitrary values, though, you'll be able to do this.
In the meantime, we should report the example you gave as an error.
from carp.
I see. So there is no way to get a struct's value without it being behind a reference?
from carp.
Yes that's right. You can always pass an inline reference to the function if you have an actual value, e.g. in your example:
(defn extract [s]
@(Struct.x &s))
Note that the field access also returns a reference. We have to do it this way because moving ownership of the field out of the struct could lead to memory problems. If we eventually give structs lifetime information we might be able to have variations that pass ownership. Note though, that the compiler is smart about these copies. For instance, even though you're "copying" the field in the example I gave above, in the compiled code it'll more than likely just be a pointer dereference and not an actual allocation.
from carp.
But then, what about tuples?
They are product types, which can be pattern-matched.
I've noticed browsing the Core library that a private macro called deftuple or similar is available. Shouldn't pattern-matchable product types be possible to define by users?
from carp.
Shouldn't pattern-matchable product types be possible to define by users?
Yes, but it isn't implemented yet. Eventually, we should also support pattern matching directly against literal values like: (match x 1 ... 2 ....)
.
But then, what about tuples?
They are product types, which can be pattern-matched.
Yep, this is true in most languages, but if you try this on the Carp tuple types like Pair
it won't work for the same reason your initial code didn't work, we don't support pattern matching on product types yet.
from carp.
What. I thought I tried it and it worked.
Lemme check.
Nope, i was wrong, you're right. Closing.
from carp.
Related Issues (20)
- memory management crash
- performance: better default string functions
- Bug with REPL displaying result of calling function that returns Char HOT 1
- REPL prompt is not `鯉` on Linux HOT 2
- (()) seems to cause an infinite loop on the latest release
- Packaging issues. HOT 1
- `format` leaks memory HOT 1
- `--eval-preload` does not have access to Core anymore HOT 6
- [Bug] Not handling all cases during pattern-matching in lambdas HOT 8
- [Bug] Control.iterate-until double-uses a variable, and thus is broken HOT 3
- [Feat Request] Exhaustive pattern-matching for `defn` HOT 5
- Document println*
- Feature Request: Make running tests not dependent on stack?
- CarpHask fails to build with hashable >= 1.4 (GHC 9.2 / Stackage LTS-20) HOT 4
- FR: Better Support for Embedded HOT 5
- Get Can't find symbol 'make-update-deriver' when trying to run example on Windows 11 HOT 7
- [bug] duplicate bindings in environment HOT 2
- Repl crashes on type of Map
- Repl :t command doesn't work with (use) HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from carp.