Coder Social home page Coder Social logo

Comments (7)

AZMCode avatar AZMCode commented on May 27, 2024

If necessary I can also include the output C files

from carp.

scolsen avatar scolsen commented on May 27, 2024

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.

AZMCode avatar AZMCode commented on May 27, 2024

I see. So there is no way to get a struct's value without it being behind a reference?

from carp.

scolsen avatar scolsen commented on May 27, 2024

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.

AZMCode avatar AZMCode commented on May 27, 2024

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.

scolsen avatar scolsen commented on May 27, 2024

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.

AZMCode avatar AZMCode commented on May 27, 2024

What. I thought I tried it and it worked.
Lemme check.
Nope, i was wrong, you're right. Closing.

from carp.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.