Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Activity
  • Jun 15 13:34
    Sija milestoned #437
  • Jun 15 13:26
    matthewmcgarvey synchronize #446
  • Jun 15 13:25
    Sija closed #437
  • Jun 15 12:49
    matthewmcgarvey synchronize #437
  • Jun 15 03:54
    matthewmcgarvey synchronize #437
  • Jun 14 15:12
    matthewmcgarvey synchronize #437
  • Jun 14 15:10
    matthewmcgarvey synchronize #437
  • Jun 14 15:04
    matthewmcgarvey synchronize #437
  • Jun 14 15:02
    matthewmcgarvey synchronize #437
  • Jun 14 14:59
    matthewmcgarvey synchronize #446
  • Jun 14 14:58
    matthewmcgarvey synchronize #437
  • Jun 14 14:42
    matthewmcgarvey synchronize #437
  • Jun 14 14:41
    matthewmcgarvey synchronize #446
  • Jun 13 18:01
    Sija labeled #465
  • Jun 13 18:01
    Sija labeled #465
  • Jun 13 17:59
    bbugh opened #465
  • Jun 12 18:22
    Sija closed #464
  • Jun 12 18:22
    Sija milestoned #464
  • Jun 12 18:22
    Sija labeled #464
  • Jun 12 17:43
    bbugh opened #464
Sung-Chul Lee
@sclee15
Mint rocks
Szikszai Gusztáv
@gdotdesign
Thanks :metal:
bingobango7624
@bingobango7624:matrix.org
[m]
hoohah. I'm confused about the documentation here: https://www.mint-lang.com/guide/reference/javascript-interop/encode-expression When I try to do the encode { name = "Bob" }, it says no matching record, but when I create a record type that matches, it says it can't be a record.
record Bob {
  name : String
}

store BobStore {
  fun serialize (): String {
    /* encode { name = "Bob" } */ ❌
    encode Bob("Hello")  /* ❌ */
    |> Json.stringify()
  }
}
Szikszai Gusztáv
@gdotdesign
Hi @bingobango7624:matrix.org :wave:
This is probably a bug, it should work like this https://sandbox.mint-lang.com/sandboxes/Lw0-EYwYiApA3Q
Szikszai Gusztáv
@gdotdesign
encode { name = "Bob" } |> Json.stringify() is basically the same as encode Json.stringify({ name = "Bob" })
the guide needs to be updated
bingobango7624
@bingobango7624:matrix.org
[m]
Thanks. How do you encode a record then?
And, most specifically, de/serialize a store to json to read/write in localstorage
bingobango7624
@bingobango7624:matrix.org
[m]
And, as an unimportant technical curiosity why do enums use :: instead of . for references? I keep doing it wrong. 🙃
Szikszai Gusztáv
@gdotdesign

Thanks. How do you encode a record then?

Should be the same just need to use parentheses: (encode Bob("Hello")) |> Json.stringify()

And, most specifically, de/serialize a store to json to read/write in localstorage

Have only one state - probably a record - which you can encode, read write in localstorage

And, as an unimportant technical curiosity why do enums use :: instead of . for references? I keep doing it wrong. 🙃

It was a choice to differentiate them somehow.

bingobango7624
@bingobango7624:matrix.org
[m]
Ah, the parentheses of course. It was trying to do Bob("Hello") |> Json.stringify() |> encode. Thanks!
bingobango7624
@bingobango7624:matrix.org
[m]
Is there a syntax reference somewhere? Looking for the pipe operator in particular
e.g. if it only pipes to the first argument
Found it on the functions page - ignore me
bingobango7624
@bingobango7624:matrix.org
[m]
I am confused about the documentation here under "matching an enum" https://www.mint-lang.com/guide/reference/control-expressions/case (can the documentation generate header anchors?)
fun load {
    case (Storage.Local.get("state")) {
      Result::Err error => "error"
      Result::Ok value => value
    }
  }
This says "I was looking for a branch of a case expression but found Result::Err instead."
bingobango7624
@bingobango7624:matrix.org
[m]
Seems to work when wrapping value or error in (), like Result::Err(error). Are the docs out of date or is there something else wrong?
bingobango7624
@bingobango7624:matrix.org
[m]
Ok I'm stumped again. How do you update a nexted record in a store? I've tried many different ways update, here's a few. 😕
record Items {
  widgets : Number
}

record StoreRecord {
  items : Items
}

store Store {
  state storeState : StoreRecord = StoreRecord(Items(1))

  fun updateWidgets(count : Number) {
    next { storeState | items = { items | widgets = storeState.items.widgets + value } }
    next { storeState = { storeState | items = { items | widgets = storeState.items.widgets + value } } }
    next { storeState = { storeState | items = { storeState.items | widgets = storeState.items.widgets + value } } }
  }
}
bingobango7624
@bingobango7624:matrix.org
[m]
:point_up: Edit: Ok I'm stumped again. How do you update a nested record in a store? I've tried many different ways update to no avail, here's a few. 😕
record Items {
  widgets : Number
}

record StoreRecord {
  items : Items
}

store Store {
  state storeState : StoreRecord = StoreRecord(Items(1))

  fun updateWidgets(count : Number) {
    next { storeState | items = { items | widgets = storeState.items.widgets + value } }
    next { storeState = { storeState | items = { items | widgets = storeState.items.widgets + value } } }
    next { storeState = { storeState | items = { storeState.items | widgets = storeState.items.widgets + value } } }
  }
}
Ryan Prior
@ryanprior
I think you might need to do all of those in one next block.
bingobango7624
@bingobango7624:matrix.org
[m]
Thanks - not trying to do all of them, trying to do just one. Those were the different attempts I made to make it work, none of which work :/
ryanprior
@ryanprior:matrix.org
[m]
Okay dang, how about something like
next { storeState.items.widgets = storeState.items.widgets + value }
Can you not do that because records are immutable even in a next?
bingobango7624
@bingobango7624:matrix.org
[m]
Good thought, but that is a similar error to the other ones:
The name of a field and its value must be separated by an equal sign

I was looking for the equal sign = but found .items.widgets instead.
I also tried defining a function named items and widgets that return the expected value, like Elm would do, and that didn't work.
ryanprior
@ryanprior:matrix.org
[m]
bingobango7624: I found a solution, it's kinda ugly but maybe there's a better way (or perhaps we can create one =D)
bingobango7624
@bingobango7624:matrix.org
[m]
Thanks, that does work but is kinda ugly 😅
  fun updateWidgets(count : Number) {
    next { storeState = newStoreState }
  } where {
    oldItems = storeState.items
    newItems = { oldItems | widgets = oldItems.widgets + count }
    newStoreState = { storeState | items = newItems }
  }
It would be easier to use a flatter model for the store than a single state item of records, but that makes serialization harder, and doesn't entirely solve the problem because there will still be nested records somewhere.
Ryan Prior
@ryanprior
Yes, we need a reasonable model for updating nested data =X
bingobango7624
@bingobango7624:matrix.org
[m]
This works too, but adds unwanted extra functions to the store API. If record could have "helper" functions like Elm's top-level sort of helper functions, it might be nicer organization...
store Store {
  state storeState : StoreRecord = StoreRecord(Items(1))

  /* Don't want this on the Store */
  fun addWidgets(items : Items, count : Number) {
    { items | widgets = items.widgets + count }
  }

  fun updateWidgets(count : Number) {
    next { storeState = { storeState | items = addWidgets(storeState.items, count) } }
  }
}
Or something :)
Certainly, easy nested updates would be nice
Ryan Prior
@ryanprior
I love the API of immutability-helper, which also works well with Typescript to make it type-safe: https://github.com/kolodny/immutability-helper
Would love to see something similar in Mint
I also like Clojure's API for updating nested structures: https://clojuredocs.org/clojure.core/update-in
bingobango7624
@bingobango7624:matrix.org
[m]
I like update-in in clojure, reminds me of Elixir's superb stdlib. There's no Record module but I can imagine something like Record.updateIn(r, {"items", "widgets"}, 3), and/or with a lambda Record.updateIn(r, {"items", "widgets"}, (value : Number) : Number => { value + 5 })
Ryan Prior
@ryanprior
What I love about immutability-helper's API is how the shape of the update mirrors the shape of the data, and you can make many updates at once atomically.
There was a talk at StrangeLoop about creating a similar API for Clojure, I'll have to track it down.
bingobango7624
@bingobango7624:matrix.org
[m]
I'd used immutability-helper before and wasn't a fan, but I think it was more because it used to be JavaScript that was later converted to TypeScript which always has warts. I imagine from scratch with an enum instead of magic keys like $push could have a good result.
Ryan Prior
@ryanprior
The TypeScript version makes quite advanced use of the type system, the keys are effectively enums (it'll complain if you try to $puhs for example) and the type system can infer the shape of the resulting updated object.
I'd agree it's not ideal, I think it's quite good given what JavaScript gives us to work with.
ryanprior
@ryanprior:matrix.org
[m]
And the library it's about: https://github.com/noprompt/meander
bingobango7624
@bingobango7624:matrix.org
[m]
I found out by accident that you can have a record and a module with the same name, so doing things like Items.addWidgets is possible. It does simplify the code and keeps the logic in the same domainc.
module Items {
  fun addWidgets (items : Items, count : Number) {
    { items | widgets = items.widgets + count }
  }
}

store Store {
  fun updateWidgets (count : Number) {
    next { storeState = { storeState | items = Items.addWidgets(storeState.items, count) } }
  }
}
ryanprior
@ryanprior:matrix.org
[m]

What's the story on encoding/decoding nontrivial types? For example I have a type like:

enum Message.Part {
  Text(String)
  Numbers(Array(Number))
}

record Actor {
  name : String
}

record Message {
  from : Actor,
  parts : Array(Message.Part)
}

When I try to decode one of these bad boys like message = decode messageJsObject as Message I get a type error saying "This type cannot be automatically decoded"

How do I manually decode it? Or what am I supposed to do in this case?