#[Exercism] what `treasure` is doing here?

1 messages · Page 1 of 1 (latest)

zealous flax
#

this is related to the treasure challenge on exercism:
https://exercism.org/tracks/gleam/exercises/treasure-chest/edit

and there is this part of the code:

pub fn get_treasure(
  chest: TreasureChest(treasure),  
  password: String) 
  -> UnlockResult(treasure) {  
}

i'm not sure what treasure in chest: TreasureChest(treasure) is doing. Maybe is a generic, but then i'm not sure where is coming from. I feel like i'm having a misunderstanding of how things work here

deft knot
#

That's right, TreasureChest and UnlockResult are generic types

zealous flax
deft knot
#

It's just normal generics

#

In Gleam it's () rather than the <> you'd find in languages like Java

#

Otherwise it is largely the same

zealous flax
deft knot
#
pub fn get_treasure(
  chest: TreasureChest(treasure),  
  password: String) 
  -> UnlockResult(treasure) {  
}

In TypeScript syntax it would be this

export function getTreasure<Treasure>(
  chest: TreasureChest<Treasure>,
  password: String,
): UnlockResult<Treasure> {
}
#

treasure is a type variable, so the get_treasure function works with TreasureChest types of any type as their parameter

zealous flax
#

is there anywhere where I can read about the reason why using () instead of <>? I feel like using <> would make easier to read and allow a shorthand for types variants that have the same name to be written easier. something like how Kotlin does:

pub type Cat(name: String)

instead of

pub type Cat {
  Cat(name: String)
}

but I don't know the reason why the other way, so i'm curious

deft knot
#

The reason is to make the language simpler

#

No reason to have a different syntax for calling type constructors and value constructors

#

A constructor is a constructor, and there's only one syntax for it

#

No particular reason to copy Java, other than being more familiar to folks coming from Java inspired languages