Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6567777
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T14:22:38+00:00 2026-05-25T14:22:38+00:00

Despite the title I’m not going to ask about a mere translation between OO

  • 0

Despite the title I’m not going to ask about a mere translation between OO world and Haskell, but I can’t figure out a better title. This discussion is similar, but not equal, to this one.

I’ve started a toy project just to expand my limited knowledge of Haskell while reading "Learn You a Haskell for a Great Good", and I’ve decided to implement a very basic "Elemental Type System", which is a subset of a typical battle system in games like Final Fantasy et simila.
I’m skipping most of the details, but this is in a nutshell my problem:

I want to model a spell, a magic you can cast on the player or on a monster. In the OO world you usually go for a "Castable" interface with a method "onCast(Player)", a "Spell" class so you can define thing like this

Spell myNewSpell = Spell("Fire", 100, 20);
myNewSpell.onCast(Player p); //models the behaviour for the Fire spell

In Haskell I thought this in terms of Types and Classes (I know that Classes in Haskell are a different concept!). I’ve encountered some difficulties, because my first attempt was to create this:

--A type synonim, a tuple (HP,MP)
type CastResult = (Integer,Integer)


--A castable spell can either deal damage (or restore) or
--inflict a status
class Castable s where
  onCast :: s -> Either (Maybe Status) CastResult


data Spell = Spell{spellName :: String,
                   spellCost :: Integer,
                   spellHpDmg :: Integer,
                   spellMpDmg :: Integer,
                   spellElem :: Maybe Element} deriving (Eq,Show,Read)

Now suppose I create some spell using the Record Syntax

bio = Spell{spellName = "Bio", ...etc..}

I would like be able to do something like this

instance Castable bio where
  onCast bio = Left (Just Poison)

There are many problems here:

  1. I can’t do "Castable bio" since bio must be a concrete type, not a value of the Type (It should be Castable Spell)

  2. bio isn’t in scope, inside the instance block is seen just as a value to pattern match against

Overall, I feel this choice of design is pretty poor, but I’m still learning and I don’t grasp such advanced topics like Functors, just to name one.

In a nutshell, which is the idiomatic way to dealing with situation like this? I mean situation which requires "one definition, multiple implementation for multiple instances", just to use the OO terminology.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T14:22:39+00:00Added an answer on May 25, 2026 at 2:22 pm

    Type classes are useful when you’re dealing with different types. In this case, however, it seems to me like you’re dealing with separate instances. In such a case, it’s probably simplest to have the cast function be just another record field.

    data Spell = Spell{spellName :: String,
                       ...
                       onCast :: Either (Maybe Status) CastResult }
        deriving (Eq,Show,Read)
    
    bio = Spell { spellName = "Bio", onCast = Left (Just Poison), ... } 
    

    Or you could do something that models your requirements more explicitly, using domain-specific types rather than generic ones like Either.

    type ManaPoints = Integer
    type HitPoints  = Integer
    
    data Spell = Spell { spellName :: String,
                         spellCost :: ManaPoints,
                         spellElem :: Maybe Element,
                         spellEffect :: Effect }
    
    data Effect = Damage  HitPoints ManaPoints
                | Inflict Status
    
    cast :: Spell -> Player -> Player
    cast spell player =
        case spellEffect spell of
            Damage hp mana = ...
            Inflict status = ...
    
    bio  = Spell { spellName = "Bio", spellEffect = Inflict Poison, ... }
    fire = Spell { spellName = "Fire", spellEffect = Damage 100 0, ... }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know very little about JavaScript but despite this I'm trying to cobble something
I have no clue about trigonometry, despite learning it in school way back when,
This one is interesting to me - despite the almost inane title. I have
This is not a duplicate of this post although the title is very similar.
Despite primarily being a windows user, I am a huge fan of rsync. Now,
Despite an earlier question ( asked here ), our project is constrained to using
Despite having very little Linux experience, I'm too enticed by VPS (and too sick
Despite this being one of the best error messages I've ever seen (second only
Despite enabling WSE 3.0 on client projects in Visual Studio 2005, web references I
Why are SQL distributions so non-standard despite an ANSI standard existing for SQL? Are

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.